| 1 |
use { |
| 2 |
crate::{ |
| 3 |
Affine2, Affine3A, BVec2, BVec3, BVec4, DAffine2, DAffine3, DMat2, DMat3, DMat4, DQuat, |
| 4 |
DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat3A, Mat4, Quat, UVec2, UVec3, |
| 5 |
UVec4, Vec2, Vec3, Vec3A, Vec4, |
| 6 |
}, |
| 7 |
speedy::{Context, Readable, Reader, Writable, Writer}, |
| 8 |
}; |
| 9 |
|
| 10 |
macro_rules! impl_for_vec { |
| 11 |
($T:ty, $ctor:ident, $comp_ty:ty, $comp_read_fn:ident, $comp_write_fn:ident, $($comp:ident),+) => { |
| 12 |
impl<'a, C> Readable<'a, C> for $T |
| 13 |
where |
| 14 |
C: Context, |
| 15 |
{ |
| 16 |
#[inline] |
| 17 |
fn read_from<R: Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> { |
| 18 |
$( |
| 19 |
let $comp = reader.$comp_read_fn()?; |
| 20 |
)+ |
| 21 |
|
| 22 |
Ok(<$T>::$ctor($($comp),+)) |
| 23 |
} |
| 24 |
|
| 25 |
#[inline] |
| 26 |
fn minimum_bytes_needed() -> usize { |
| 27 |
let mut size = 0; |
| 28 |
$( |
| 29 |
let $comp = <$comp_ty as Readable::<'a, C>>::minimum_bytes_needed(); |
| 30 |
size += $comp; |
| 31 |
)+ |
| 32 |
size |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl<C> Writable<C> for $T |
| 37 |
where |
| 38 |
C: Context, |
| 39 |
{ |
| 40 |
#[inline] |
| 41 |
fn write_to<T: ?Sized + Writer<C>>(&self, writer: &mut T) -> Result<(), C::Error> { |
| 42 |
$( |
| 43 |
writer.$comp_write_fn(self.$comp)?; |
| 44 |
)+ |
| 45 |
|
| 46 |
Ok(()) |
| 47 |
} |
| 48 |
|
| 49 |
#[inline] |
| 50 |
fn bytes_needed(&self) -> Result<usize, C::Error> { |
| 51 |
let mut size = 0; |
| 52 |
$( |
| 53 |
size += Writable::<C>::bytes_needed(&self.$comp)?; |
| 54 |
)+ |
| 55 |
Ok( size ) |
| 56 |
} |
| 57 |
} |
| 58 |
}; |
| 59 |
} |
| 60 |
|
| 61 |
impl_for_vec! {Vec2, new, f32, read_f32, write_f32, x, y} |
| 62 |
impl_for_vec! {Vec3, new, f32, read_f32, write_f32, x, y, z} |
| 63 |
impl_for_vec! {Vec3A, new, f32, read_f32, write_f32, x, y, z} |
| 64 |
impl_for_vec! {Vec4, new, f32, read_f32, write_f32, x, y, z, w} |
| 65 |
|
| 66 |
impl_for_vec! {DVec2, new, f64, read_f64, write_f64, x, y} |
| 67 |
impl_for_vec! {DVec3, new, f64, read_f64, write_f64, x, y, z} |
| 68 |
impl_for_vec! {DVec4, new, f64, read_f64, write_f64, x, y, z, w} |
| 69 |
|
| 70 |
impl_for_vec! {IVec2, new, i32, read_i32, write_i32, x, y} |
| 71 |
impl_for_vec! {IVec3, new, i32, read_i32, write_i32, x, y, z} |
| 72 |
impl_for_vec! {IVec4, new, i32, read_i32, write_i32, x, y, z, w} |
| 73 |
|
| 74 |
impl_for_vec! {UVec2, new, u32, read_u32, write_u32, x, y} |
| 75 |
impl_for_vec! {UVec3, new, u32, read_u32, write_u32, x, y, z} |
| 76 |
impl_for_vec! {UVec4, new, u32, read_u32, write_u32, x, y, z, w} |
| 77 |
|
| 78 |
impl_for_vec! {Quat, from_xyzw, f32, read_f32, write_f32, x, y, z, w} |
| 79 |
impl_for_vec! {DQuat, from_xyzw, f64, read_f64, write_f64, x, y, z, w} |
| 80 |
|
| 81 |
macro_rules! impl_for_bvec { |
| 82 |
($T:ty, $($comp:ident),+) => { |
| 83 |
impl<'a, C> Readable<'a, C> for $T |
| 84 |
where |
| 85 |
C: Context, |
| 86 |
{ |
| 87 |
#[inline] |
| 88 |
#[allow(unused_assignments)] |
| 89 |
fn read_from<R: Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> { |
| 90 |
let mask = reader.read_u8()?; |
| 91 |
let mut shift = 0; |
| 92 |
|
| 93 |
$( |
| 94 |
let $comp = (mask & (1 << shift)) != 0; |
| 95 |
shift += 1; |
| 96 |
)+ |
| 97 |
|
| 98 |
Ok(<$T>::new($($comp),+)) |
| 99 |
} |
| 100 |
|
| 101 |
#[inline] |
| 102 |
fn minimum_bytes_needed() -> usize { |
| 103 |
<u8 as Readable::<'a, C>>::minimum_bytes_needed() |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl<C> Writable<C> for $T |
| 108 |
where |
| 109 |
C: Context, |
| 110 |
{ |
| 111 |
#[inline] |
| 112 |
fn write_to<T: ?Sized + Writer<C>>(&self, writer: &mut T) -> Result<(), C::Error> { |
| 113 |
writer.write_u8(self.bitmask() as u8) |
| 114 |
} |
| 115 |
|
| 116 |
#[inline] |
| 117 |
fn bytes_needed(&self) -> Result<usize, C::Error> { |
| 118 |
Writable::<C>::bytes_needed(&0u8) |
| 119 |
} |
| 120 |
} |
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
impl_for_bvec! {BVec2, x, y} |
| 125 |
impl_for_bvec! {BVec3, x, y, z} |
| 126 |
impl_for_bvec! {BVec4, x, y, z, w} |
| 127 |
|
| 128 |
macro_rules! impl_for_mat { |
| 129 |
($T:ty, $comp_count:literal, $comp_ty:ty) => { |
| 130 |
impl<'a, C> Readable<'a, C> for $T |
| 131 |
where |
| 132 |
C: Context, |
| 133 |
{ |
| 134 |
#[inline] |
| 135 |
fn read_from<R: Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> { |
| 136 |
let mut values = [Default::default(); $comp_count]; |
| 137 |
for v in &mut values { |
| 138 |
*v = reader.read_value()?; |
| 139 |
} |
| 140 |
Ok(<$T>::from_cols_array(&values)) |
| 141 |
} |
| 142 |
|
| 143 |
#[inline] |
| 144 |
fn minimum_bytes_needed() -> usize { |
| 145 |
<$comp_ty as Readable<'a, C>>::minimum_bytes_needed() * $comp_count |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
impl<C> Writable<C> for $T |
| 150 |
where |
| 151 |
C: Context, |
| 152 |
{ |
| 153 |
#[inline] |
| 154 |
fn write_to<T: ?Sized + Writer<C>>(&self, writer: &mut T) -> Result<(), C::Error> { |
| 155 |
for comp in self.to_cols_array().iter() { |
| 156 |
writer.write_value(comp)? |
| 157 |
} |
| 158 |
|
| 159 |
Ok(()) |
| 160 |
} |
| 161 |
|
| 162 |
#[inline] |
| 163 |
fn bytes_needed(&self) -> Result<usize, C::Error> { |
| 164 |
let mut size = 0; |
| 165 |
for comp in self.to_cols_array().iter() { |
| 166 |
size += Writable::<C>::bytes_needed(comp)?; |
| 167 |
} |
| 168 |
Ok(size) |
| 169 |
} |
| 170 |
} |
| 171 |
}; |
| 172 |
} |
| 173 |
|
| 174 |
impl_for_mat! { Mat2, 4, f32 } |
| 175 |
impl_for_mat! { Mat3, 9, f32 } |
| 176 |
impl_for_mat! { Mat3A, 9, f32 } |
| 177 |
impl_for_mat! { Mat4, 16, f32 } |
| 178 |
|
| 179 |
impl_for_mat! { DMat2, 4, f64 } |
| 180 |
impl_for_mat! { DMat3, 9, f64 } |
| 181 |
impl_for_mat! { DMat4, 16, f64 } |
| 182 |
|
| 183 |
impl_for_mat! { Affine2, 6, f32 } |
| 184 |
impl_for_mat! { Affine3A, 12, f32 } |
| 185 |
|
| 186 |
impl_for_mat! { DAffine2, 6, f64 } |
| 187 |
impl_for_mat! { DAffine3, 12, f64 } |
| 188 |
|
| 189 |
#[test] |
| 190 |
fn test_speedy() { |
| 191 |
use speedy::Endianness; |
| 192 |
|
| 193 |
macro_rules! test_vec { |
| 194 |
($T:ty, $ctor:ident, $($values:literal),+) => {{ |
| 195 |
let original = <$T>::$ctor($($values as _),+); |
| 196 |
let serialized = original.write_to_vec_with_ctx(Endianness::NATIVE).unwrap(); |
| 197 |
let deserialized = <$T>::read_from_buffer_with_ctx(Endianness::NATIVE, &serialized).unwrap(); |
| 198 |
assert_eq!(original, deserialized); |
| 199 |
}} |
| 200 |
} |
| 201 |
|
| 202 |
test_vec!(Vec2, new, 1, 2); |
| 203 |
test_vec!(Vec3, new, 1, 2, 3); |
| 204 |
test_vec!(Vec3A, new, 1, 2, 3); |
| 205 |
test_vec!(Vec4, new, 1, 2, 3, 4); |
| 206 |
|
| 207 |
test_vec!(DVec2, new, 1, 2); |
| 208 |
test_vec!(DVec3, new, 1, 2, 3); |
| 209 |
test_vec!(DVec4, new, 1, 2, 3, 4); |
| 210 |
|
| 211 |
test_vec!(IVec2, new, 1, 2); |
| 212 |
test_vec!(IVec3, new, 1, 2, 3); |
| 213 |
test_vec!(IVec4, new, 1, 2, 3, 4); |
| 214 |
|
| 215 |
test_vec!(UVec2, new, 1, 2); |
| 216 |
test_vec!(UVec3, new, 1, 2, 3); |
| 217 |
test_vec!(UVec4, new, 1, 2, 3, 4); |
| 218 |
|
| 219 |
test_vec!(Quat, from_xyzw, 1, 2, 3, 4); |
| 220 |
test_vec!(DQuat, from_xyzw, 1, 2, 3, 4); |
| 221 |
|
| 222 |
for a in [false, true] { |
| 223 |
for b in [false, true] { |
| 224 |
let original = BVec2::new(a, b); |
| 225 |
let serialized = original.write_to_vec_with_ctx(Endianness::NATIVE).unwrap(); |
| 226 |
let deserialized = |
| 227 |
BVec2::read_from_buffer_with_ctx(Endianness::NATIVE, &serialized).unwrap(); |
| 228 |
assert_eq!(original, deserialized); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
for a in [false, true] { |
| 233 |
for b in [false, true] { |
| 234 |
for c in [false, true] { |
| 235 |
let original = BVec3::new(a, b, c); |
| 236 |
let serialized = original.write_to_vec_with_ctx(Endianness::NATIVE).unwrap(); |
| 237 |
let deserialized = |
| 238 |
BVec3::read_from_buffer_with_ctx(Endianness::NATIVE, &serialized).unwrap(); |
| 239 |
assert_eq!(original, deserialized); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
for a in [false, true] { |
| 245 |
for b in [false, true] { |
| 246 |
for c in [false, true] { |
| 247 |
for d in [false, true] { |
| 248 |
let original = BVec4::new(a, b, c, d); |
| 249 |
let serialized = original.write_to_vec_with_ctx(Endianness::NATIVE).unwrap(); |
| 250 |
let deserialized = |
| 251 |
BVec4::read_from_buffer_with_ctx(Endianness::NATIVE, &serialized).unwrap(); |
| 252 |
assert_eq!(original, deserialized); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
macro_rules! test_mat { |
| 259 |
($T:ty) => {{ |
| 260 |
let mut cols = <$T>::IDENTITY.to_cols_array(); |
| 261 |
for (i, c) in cols.iter_mut().enumerate() { |
| 262 |
*c = (i + 1) as _; |
| 263 |
} |
| 264 |
|
| 265 |
let original = <$T>::from_cols_array(&cols); |
| 266 |
let serialized = original.write_to_vec_with_ctx(Endianness::NATIVE).unwrap(); |
| 267 |
let deserialized = |
| 268 |
<$T>::read_from_buffer_with_ctx(Endianness::NATIVE, &serialized).unwrap(); |
| 269 |
assert_eq!(original, deserialized); |
| 270 |
}}; |
| 271 |
} |
| 272 |
|
| 273 |
test_mat!(Mat2); |
| 274 |
test_mat!(Mat3); |
| 275 |
test_mat!(Mat3A); |
| 276 |
test_mat!(Mat4); |
| 277 |
|
| 278 |
test_mat!(DMat2); |
| 279 |
test_mat!(DMat3); |
| 280 |
test_mat!(DMat4); |
| 281 |
|
| 282 |
test_mat!(Affine2); |
| 283 |
test_mat!(Affine3A); |
| 284 |
|
| 285 |
test_mat!(DAffine2); |
| 286 |
test_mat!(DAffine3); |
| 287 |
} |