1use rancor::Fallible;
2
3use crate::{
4 rend::*, traits::CopyOptimization, Archive, Deserialize, Place, Serialize,
5};
6
7macro_rules! impl_rend_primitive {
8 ($type:ty) => {
9 impl Archive for $type {
10 const COPY_OPTIMIZATION: CopyOptimization<Self> =
11 unsafe { CopyOptimization::enable() };
12
13 type Archived = Self;
14 type Resolver = ();
15
16 #[inline]
17 fn resolve(&self, _: Self::Resolver, out: Place<Self::Archived>) {
18 out.write(*self);
19 }
20 }
21
22 impl<S: Fallible + ?Sized> Serialize<S> for $type {
23 fn serialize(&self, _: &mut S) -> Result<Self::Resolver, S::Error> {
24 Ok(())
25 }
26 }
27
28 impl<D: Fallible + ?Sized> Deserialize<$type, D> for $type {
29 fn deserialize(&self, _: &mut D) -> Result<$type, D::Error> {
30 Ok(*self)
31 }
32 }
33 };
34}
35
36macro_rules! impl_rend_primitives {
37 ($($type:ty),* $(,)?) => {
38 $(impl_rend_primitive!($type);)*
39 };
40}
41
42impl_rend_primitives!(
43 i16_le,
44 i32_le,
45 i64_le,
46 i128_le,
47 u16_le,
48 u32_le,
49 u64_le,
50 u128_le,
51 f32_le,
52 f64_le,
53 char_le,
54 NonZeroI16_le,
55 NonZeroI32_le,
56 NonZeroI64_le,
57 NonZeroI128_le,
58 NonZeroU16_le,
59 NonZeroU32_le,
60 NonZeroU64_le,
61 NonZeroU128_le,
62 i16_be,
63 i32_be,
64 i64_be,
65 i128_be,
66 u16_be,
67 u32_be,
68 u64_be,
69 u128_be,
70 f32_be,
71 f64_be,
72 char_be,
73 NonZeroI16_be,
74 NonZeroI32_be,
75 NonZeroI64_be,
76 NonZeroI128_be,
77 NonZeroU16_be,
78 NonZeroU32_be,
79 NonZeroU64_be,
80 NonZeroU128_be,
81);
82
83#[cfg(test)]
84mod tests {
85 use rend::*;
86
87 use crate::api::test::{roundtrip, to_bytes};
88
89 #[test]
90 fn roundtrip_integers() {
91 roundtrip(&i16_be::from_native(12345i16));
92 roundtrip(&i32_be::from_native(1234567890i32));
93 roundtrip(&i64_be::from_native(1234567890123456789i64));
94 roundtrip(&i128_be::from_native(
95 123456789012345678901234567890123456789i128,
96 ));
97 roundtrip(&u16_be::from_native(12345u16));
98 roundtrip(&u32_be::from_native(1234567890u32));
99 roundtrip(&u64_be::from_native(12345678901234567890u64));
100 roundtrip(&u128_be::from_native(
101 123456789012345678901234567890123456789u128,
102 ));
103
104 roundtrip(&i16_le::from_native(12345i16));
105 roundtrip(&i32_le::from_native(1234567890i32));
106 roundtrip(&i64_le::from_native(1234567890123456789i64));
107 roundtrip(&i128_le::from_native(
108 123456789012345678901234567890123456789i128,
109 ));
110 roundtrip(&u16_le::from_native(12345u16));
111 roundtrip(&u32_le::from_native(1234567890u32));
112 roundtrip(&u64_le::from_native(12345678901234567890u64));
113 roundtrip(&u128_le::from_native(
114 123456789012345678901234567890123456789u128,
115 ));
116 }
117
118 #[test]
119 fn roundtrip_floats() {
120 roundtrip(&f32_be::from_native(1234567f32));
121 roundtrip(&f64_be::from_native(12345678901234f64));
122
123 roundtrip(&f32_le::from_native(1234567f32));
124 roundtrip(&f64_le::from_native(12345678901234f64));
125 }
126
127 #[test]
128 fn roundtrip_chars() {
129 roundtrip(&char_be::from_native('x'));
130 roundtrip(&char_be::from_native('🥺'));
131
132 roundtrip(&char_le::from_native('x'));
133 roundtrip(&char_le::from_native('🥺'));
134 }
135
136 #[test]
137 fn roundtrip_nonzero() {
138 roundtrip(&NonZeroI16_be::new(12345).unwrap());
139 roundtrip(&NonZeroI32_be::new(1234567890).unwrap());
140 roundtrip(&NonZeroI64_be::new(1234567890123456789).unwrap());
141 roundtrip(
142 &NonZeroI128_be::new(123456789012345678901234567890123456789)
143 .unwrap(),
144 );
145 roundtrip(&NonZeroU16_be::new(12345).unwrap());
146 roundtrip(&NonZeroU32_be::new(1234567890).unwrap());
147 roundtrip(&NonZeroU64_be::new(1234567890123456789).unwrap());
148 roundtrip(
149 &NonZeroU128_be::new(123456789012345678901234567890123456789)
150 .unwrap(),
151 );
152
153 roundtrip(&NonZeroI16_le::new(12345).unwrap());
154 roundtrip(&NonZeroI32_le::new(1234567890).unwrap());
155 roundtrip(&NonZeroI64_le::new(1234567890123456789).unwrap());
156 roundtrip(
157 &NonZeroI128_le::new(123456789012345678901234567890123456789)
158 .unwrap(),
159 );
160 roundtrip(&NonZeroU16_le::new(12345).unwrap());
161 roundtrip(&NonZeroU32_le::new(1234567890).unwrap());
162 roundtrip(&NonZeroU64_le::new(1234567890123456789).unwrap());
163 roundtrip(
164 &NonZeroU128_le::new(123456789012345678901234567890123456789)
165 .unwrap(),
166 );
167 }
168
169 #[test]
170 fn verify_endianness() {
171 let value = i32_be::from_native(0x12345678);
173 to_bytes(&value, |buf| {
174 assert_eq!(&buf[0..4], &[0x12, 0x34, 0x56, 0x78]);
175 });
176
177 let value = i32_le::from_native(0x12345678i32);
179 to_bytes(&value, |buf| {
180 assert_eq!(&buf[0..4], &[0x78, 0x56, 0x34, 0x12]);
181 });
182 }
183}