macro_rules! tinfo {
($ty:ty) => { ... };
($ty:ty, $name:literal) => { ... };
}Expand description
Creates TypeInfo from the given type and reflects whether the type
implements Send, Sync, Default, and Clone to the TypeInfo.
If you want your own type name, you can call this macro like
tinfo!(T, "new-name").
This macro exploits Rust’s function look-up procedures to determine if the
type implements the traits. See TypeHelper for more details.
§Examples
use my_ecs::prelude::*;
// - Clone detection
struct A;
struct B;
#[derive(Clone)]
struct C;
#[derive(Clone)]
struct D;
let a = tinfo!(A); // for non-cloneable type A.
let b = tinfo!(B); // for non-cloneable type B.
let c = tinfo!(C); // for cloneable type C.
let d = tinfo!(D); // for cloneable type D.
assert_eq!(a.fn_clone, b.fn_clone); // A and B have the same dummy clone function.
assert_ne!(a.fn_clone, c.fn_clone); // But C has its own clone function.
assert_ne!(a.fn_clone, d.fn_clone); // And so does D.
assert_ne!(c.fn_clone, d.fn_clone);
// - Send & Sync detection
struct SendSync(u8); // Both Send and Sync.
struct NotSendSync(*mut u8); // Neither Send nor Sync.
let send_sync = tinfo!(SendSync);
let not_send_sync = tinfo!(NotSendSync);
assert!(send_sync.is_send);
assert!(send_sync.is_sync);
assert!(!not_send_sync.is_send);
assert!(!not_send_sync.is_sync);
// Incorrect usage
fn is_clone<T: 'static>() -> bool {
// The macro doesn't work if the type is passed through generic
// parameter.
tinfo!(T).is_clone
}
// assert!(is_clone::<C>());