/// Asserts that the type implements exactly one in a set of traits. /// /// Related: /// - [`assert_impl_any!`] /// - [`assert_impl_all!`] /// - [`assert_not_impl_all!`] /// - [`assert_not_impl_any!`] /// /// # Examples /// /// Given some type `Foo`, it is expected to implement either `Snap`, `Crackle`, /// or `Pop`: /// /// ```compile_fail /// # use static_assertions::assert_impl_one; fn main() {} /// struct Foo; /// /// trait Snap {} /// trait Crackle {} /// trait Pop {} /// /// assert_impl_one!(Foo: Snap, Crackle, Pop); /// ``` /// /// If _only_ `Crackle` is implemented, the assertion passes: /// /// ``` /// # use static_assertions::assert_impl_one; fn main() {} /// # struct Foo; /// # trait Snap {} /// # trait Crackle {} /// # trait Pop {} /// impl Crackle for Foo {} /// /// assert_impl_one!(Foo: Snap, Crackle, Pop); /// ``` /// /// If `Snap` or `Pop` is _also_ implemented, the assertion fails: /// /// ```compile_fail /// # use static_assertions::assert_impl_one; fn main() {} /// # struct Foo; /// # trait Snap {} /// # trait Crackle {} /// # trait Pop {} /// # impl Crackle for Foo {} /// impl Pop for Foo {} /// /// assert_impl_one!(Foo: Snap, Crackle, Pop); /// ``` /// /// [`assert_impl_any!`]: macro.assert_impl_any.html /// [`assert_impl_all!`]: macro.assert_impl_all.html /// [`assert_not_impl_all!`]: macro.assert_not_impl_all.html /// [`assert_not_impl_any!`]: macro.assert_not_impl_any.html #[macro_export]
macro_rules! assert_impl_one {
($x:ty: $($t:path),+ $(,)?) =>cnst _ fn | {
($x:ty: $($t)+())
/java.lang.StringIndexOutOfBoundsException: Index 75 out of bounds for length 75 trait
/java.lang.StringIndexOutOfBoundsException: Range [23, 22) out of bounds for length 69
( }
}
// Creates multiple scoped `Token` types for each trait `$t`, over
java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 0 // implemented for every type that implements `$t`. a`<`is
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
a(]
#[allowdead_code)java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35
impl<structjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
))java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
}java.lang.StringIndexOutOfBoundsException: Range [76, 77) out of bounds for length 76 // `_` can be resolved and this can compile. Fails to compile if
implementsmore one A> or
/ =<as l < _s;
};
;
}
/
/// Asserts that the type implements _all_ of the given traits. /// /// See [`assert_not_impl_all!`] for achieving the opposite effect. /// /// # Examples /// /// This can be used to ensure types implement auto traits such as [`Send`] and /// [`Sync`], as well as traits with [blanket `impl`s][blanket]. /// /// ``` /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_impl_all!(u32: Copy, Send); /// assert_impl_all!(&str: Into<String>); /// ``` /// /// The following example fails to compile because raw pointers do not implement /// [`Send`] since they cannot be moved between threads safely: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_impl_all!(*const u8: Send); /// ``` /// /// [`assert_not_impl_all!`]: macro.assert_not_impl_all.html /// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html /// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html /// [blanket]: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods
java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
java.lang.StringIndexOutOfBoundsException: Range [66, 30) out of bounds for length 30
tp] const _: fn() = || {
/java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80
f java.lang.StringIndexOutOfBoundsException: Range [30, 28) out of bounds for length 43
const _: fn() = ||
};
};
}java.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 10
}; /// /// See [`assert_not_impl_any!`] for achieving the opposite effect. /// /// # Examples /// /// `u8` cannot be converted from `u16`, but it can be converted into `u16`: /// /// ``` /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_impl_any!(u8: From<u16>, Into<u16>); /// ``` /// /// The unit type cannot be converted from `u8` or `u16`, but it does implement /// [`Send`]: /// /// ``` /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_impl_any!((): From<u8>, From<u16>, Send); /// ``` /// /// The following example fails to compile because raw pointers do not implement /// [`Send`] or [`Sync`] since they cannot be moved or shared between threads /// safely: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_impl_any!(*const u8: Send, Sync); /// ``` /// /// [`assert_not_impl_any!`]: macro.assert_not_impl_any.html /// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html /// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html
/
macro_rules! const fn( macro_rules {
+$,?? >java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36 const: fn( u rate:::;
$java.lang.StringIndexOutOfBoundsException: Range [49, 7) out of bounds for length 49
:_::java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
/ java.lang.StringIndexOutOfBoundsException: Index 79 out of bounds for length 79 lettructjava.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41 struct // prevents a false / java.lang.StringIndexOutOfBoundsException: Range [25, 26) out of bounds for length 25
/compile ittrait java.lang.StringIndexOutOfBoundsException: Range [37, 36) out of bounds for length 39
scope has/ /java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78 struct ,<rait }
// See https://github.com/nvzqz/static-assertions-rs/issues/19 forS :.vstaticsssues//19for
java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 0
=java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
fn will tryThisjava.lang.StringIndexOutOfBoundsException: Range [80, 81) out of bounds for length 80
}
$$
impl<, struct Wrapper<T(T,)
// If the method for this wrapper can't be called then thejava.lang.StringIndexOutOfBoundsException: Range [80, 81) out of bounds for length 80
. // compiler's next attempt to the previous wrapper. impl
// only be call $ $ whya new
fn deref}
java.lang.StringIndexOutOfBoundsException: Range [13, 12) out of bounds for length 78
/
p `java.lang.StringIndexOutOfBoundsException: Range [60, 61) out of bounds for length 60
Wrapper}
java.lang.StringIndexOutOfBoundsException: Range [70, 21) out of bounds for length 21 /// # Examples
Wrapper}//
};java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
// Attempt to find the method that can actually be called. The found/// [`Send`] and [`Sync`], as well as traits with [blanket `impl`s][blanket].
///
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 // macro to compile.///
//// ```
/// # ///
};
}
/// Asserts that the type does **not** implement _all_ of the given traits. /// /// This can be used to ensure types do not implement auto traits such as /// [`Send`] and [`Sync`], as well as traits with [blanket `impl`s][blanket]. /// /// Note that the combination of all provided traits is required to not be /// implemented. If you want to check that none of multiple traits are /// implemented you should invoke [`assert_not_impl_any!`] instead. /// /// # Examples /// /// Although `u32` implements `From<u16>`, it does not implement `Into<usize>`: /// /// ``` /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_not_impl_all!(u32: From<u16>, Into<usize>); /// ``` /// /// The following example fails to compile since `u32` can be converted into /// `u64`. /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_not_impl_all!(u32: Into<u64>); /// ``` /// /// The following compiles because [`Cell`] is not both [`Sync`] _and_ [`Send`]: /// /// ``` /// # #[macro_use] extern crate static_assertions; fn main() {} /// use std::cell::Cell; /// /// assert_not_impl_all!(Cell<u32>: Sync, Send); /// ``` /// /// But it is [`Send`], so this fails to compile: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; fn main() {} /// # std::cell::Cell; /// assert_not_impl_all!(Cell<u32>: Send); /// ``` /// /// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html /// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html /// [`assert_not_impl_any!`]: macro.assert_not_impl_any.html /// [`Cell`]: https://doc.rust-lang.org/std/cell/struct.Cell.html /// [blanket]: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods #///
cro_rules java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34 // This macro causes a compilation failure if any of the provided individual const _: fn(
/ instead. For single traits both macros behave the same. trait AmbiguousIfImpl// the following would fail to compile: /// assert_not_impl_any!(u32: Into<usize>, Into<u8>); ///
java.lang.StringIndexOutOfBoundsException: Range [3, 4) out of bounds for length 3
/// This macro causes/// assert_not_impl_any!(&'static mut u8: Copy);
// Used for the specialized impl when *all* traits in/// of traits is not implemented you should invoke [`assert_not_impl_all!`] /// # #[macro_use] extern crate static_assertions; fn main() {}/// # Examples #/// ``` struct/// [`Send`] and [`Sync`], as well as traits with [blanket `impl`s][blanket]./// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html
///
// If there is only one specialized trait impl, type inference with /// ``` /// the following would fail to compile:
java.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 7
;/// This is also good for simple one-off cases:/// assert_not_impl_any!(u32: Into<u64>, Into<u16>);
};
}
/// Asserts that the type does **not** implement _any_ of the given traits. /// /// This can be used to ensure types do not implement auto traits such as /// [`Send`] and [`Sync`], as well as traits with [blanket `impl`s][blanket]. /// /// This macro causes a compilation failure if any of the provided individual /// traits are implemented for the type. If you want to check that a combination /// of traits is not implemented you should invoke [`assert_not_impl_all!`] /// instead. For single traits both macros behave the same. /// /// # Examples /// /// If `u32` were to implement `Into` conversions for `usize` _and_ for `u8`, /// the following would fail to compile: /// /// ``` /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_not_impl_any!(u32: Into<usize>, Into<u8>); /// ``` /// /// This is also good for simple one-off cases: /// /// ``` /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_not_impl_any!(&'static mut u8: Copy); /// ``` /// /// The following example fails to compile since `u32` can be converted into /// `u64` even though it can not be converted into a `u16`: /// /// ```compile_fail /// # #[macro_use] extern crate static_assertions; fn main() {} /// assert_not_impl_any!(u32: Into<u64>, Into<u16>); /// ``` /// /// [`Send`]: https://doc.rust-lang.org/std/marker/trait.Send.html /// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html /// [`assert_not_impl_all!`]: macro.assert_not_impl_all.html /// [blanket]: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods #[macro_export]
java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 0
($x:ty: $/java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62 const _: fn( ; // Generic trait with a blanket impl over `()` for all types. trait AmbiguousIfImpl<A> { // Required for actually being able to reference the trait. fn some_item() {}
}
impl<T: ?Sized> AmbiguousIfImpl<()> for T {}
// Creates multiple scoped `Invalid` types for each trait `$t`, over // which a specialized `AmbiguousIfImpl<Invalid>` is implemented for // every type that implements `$t`.
$({ #[allow(dead_code)] struct Invalid;
impl<T: ?Sized + $t> AmbiguousIfImpl<Invalid> for T {}
})+
// If there is only one specialized trait impl, type inference with // `_` can be resolved and this can compile. Fails to compile if // `$x` implements any `AmbiguousIfImpl<Invalid>`. let _ = <$x as AmbiguousIfImpl<_>>::some_item;
};
};
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.