-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More Assertions #2
Comments
Asserting Sync and Sendness: http://yakshav.es/asserting-static-properties/ Also, I'm still searching for a way to statically assert that a trait is object-safe. |
@skade Just added |
Here's a sample definition of a macro that would assert certain traits are implemented. macro_rules! assert_impl {
($x:ty, $($xs:ty),+) => {
$({
fn _gen<T: ?Sized + $xs>() {}
_gen::<$x>();
})+
};
($label:ident; $($xs:ty),+) => {
#[allow(dead_code, non_snake_case)]
fn $label() { assert_impl!($($xs),+); }
};
} However, when trying to use it, I get this error:
|
@skade Added |
Would be cool to see an assertion that traits are not implemented: |
There was some form of doing this that @kennytm recommend me. I don't exactly remember what it was. |
I would love to have something like
Usecase is a CLI-builder-pattern subcrate I am trying to build for imag. |
@matthiasbeyer This would likely have to be implemented as a procedural macro. In that case, both parameters would have to be string literals. I'm curious if this could be done via @iliekturtles keep an eye on #8 in case such a macro becomes implemented. |
What about a macro to assert that two types are equal? |
Could you add a macro that asserts that an enum variant exists? |
Hi, I've struct and trait that require lifetime. My first though about using assert_impl_all!(require;
Transition<'t>,
SemanticAnalyze<'t>,
From<TokenPair<'t>>,
); However, seems that's not supported. Rather I found traits need to be assert_impl_all!(require;
Transition,
SemanticAnalyze<'static>,
From<TokenPair<'static>>,
); I think this should be anonymous This also give me an idea. What about macro for asserting lifetimes? Ah yes, |
@nvzqz is there an issue for asserting |
@DrSensor the 1.0 beta is coming out soon. I'm dealing with life stuff which is why it hasn't come out yet. I tried getting around to it when 1.37 came out but I've had other priorities. Thanks for your patience! @LucioFranco if you'd like to do the leg work to assert that the type of an expression implements given traits, please make a PR! To learn how to go about what we discussed, check out how |
I am interested for a static assertion that some trait bound currently in scope is statically impossible. Example: #![feature(trivial_bounds)]
macro_rules! void {
($($tt:tt)*) => {
unreachable!() // FIXME
};
}
trait Trait {
fn f() where Self: Sized;
}
impl Trait for str {
fn f() where str: Sized {
void!(str: Sized); // evaluate to !
}
} This assertion should require that both |
@dtolnay: here is a sad version of what you ask: macro_rules! void {
($ty:ty: $tr:path) => {{
struct True;
struct False;
trait DoesImpl<M> {
fn marker() {}
}
impl<T: ?Sized> DoesImpl<False> for T {}
impl<T: ?Sized + $tr> DoesImpl<True> for T {}
const _: () = {
// Fails if `$ty: $tr` is in scope globally, because then it can't infer `M`
let _ = <$ty as DoesImpl<_>>::marker;
};
// Fails if `$ty: $tr` is not in scope locally
let _ = <$ty as DoesImpl<True>>::marker;
unreachable!()
}};
} It will only typecheck in the case where the type does not implement the trait, but the bound |
That looks fine to me. Is it something that would be a good fit for this crate? |
It would be fantastic to have static assertions added for lifetime parameter covariance/contravariance/invariance, since changing it in a public API can be a semver breakage. |
Is less than/greater than possible? More specifically, I'd be interested in |
@Kixunil isn't that already possible with |
@konsumlamm frankly, I don't remember what I wanted to use it for but I believe |
Hi, would an extension for lifetimes in the assertions be useful? I ran across an issue with traits that require lifetime parameters and have to manually write the assertion based on the expansion that the |
Discussion of other possible static assertions should occur here.
The text was updated successfully, but these errors were encountered: