as_byte_sequence/
lib.rs

1pub unsafe trait AsByteSequence: Clone {}
2
3macro_rules! impl_trait {
4    ($($ty:ty)*) => {
5        $(
6            unsafe impl $crate::AsByteSequence for $ty {}
7        )*
8    };
9}
10
11// All numeric primitive types are good
12impl_trait!(u8 i8 u16 i16 u32 i32 u64 i64 u128 i128 usize isize);
13impl_trait!(f32 f64);
14// Some special types which are good too
15// TODO: implement for ! when it is stable
16impl_trait!(());
17// We do not implement this trait automatically for pointers, because they are only valid in context
18// of one process, and sending pointer via e.g. networking is probably error
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    fn check<T: AsByteSequence>(_val: T) {}
24
25    #[test]
26    fn check_trait_is_implemented() {
27        check(3 as u8);
28        check(3 as i32);
29        check(3 as usize);
30        check(0.0 as f32);
31        check(());
32    }
33}