|
| 1 | +//! Implementations of the [`Schema`] trait for the `nalgebra` crate v0.34 |
| 2 | +
|
| 3 | +use crate::{ |
| 4 | + schema::{Data, DataModelType, NamedField}, |
| 5 | + Schema, |
| 6 | +}; |
| 7 | + |
| 8 | +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))] |
| 9 | +impl<T, const R: usize, const C: usize, S> Schema |
| 10 | + for nalgebra_v0_34::Matrix<T, nalgebra_v0_34::Const<R>, nalgebra_v0_34::Const<C>, S> |
| 11 | +where |
| 12 | + T: Schema + nalgebra_v0_34::Scalar, |
| 13 | + S: nalgebra_v0_34::base::storage::Storage< |
| 14 | + T, |
| 15 | + nalgebra_v0_34::Const<R>, |
| 16 | + nalgebra_v0_34::Const<C>, |
| 17 | + >, |
| 18 | +{ |
| 19 | + const SCHEMA: &'static DataModelType = &DataModelType::Tuple(flatten(&[[T::SCHEMA; R]; C])); |
| 20 | +} |
| 21 | + |
| 22 | +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))] |
| 23 | +impl<T: Schema + nalgebra_v0_34::Scalar, const D: usize> Schema |
| 24 | + for nalgebra_v0_34::OPoint<T, nalgebra_v0_34::Const<D>> |
| 25 | +where |
| 26 | + nalgebra_v0_34::base::default_allocator::DefaultAllocator: |
| 27 | + nalgebra_v0_34::base::allocator::Allocator<nalgebra_v0_34::Const<D>>, |
| 28 | +{ |
| 29 | + const SCHEMA: &'static DataModelType = |
| 30 | + nalgebra_v0_34::OVector::<T, nalgebra_v0_34::Const<D>>::SCHEMA; |
| 31 | +} |
| 32 | + |
| 33 | +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))] |
| 34 | +impl<T: Schema> Schema for nalgebra_v0_34::Unit<T> { |
| 35 | + const SCHEMA: &'static DataModelType = T::SCHEMA; |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))] |
| 39 | +impl<T: Schema + nalgebra_v0_34::Scalar> Schema for nalgebra_v0_34::Quaternion<T> { |
| 40 | + const SCHEMA: &'static DataModelType = nalgebra_v0_34::Vector4::<T>::SCHEMA; |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))] |
| 44 | +impl<T: Schema + nalgebra_v0_34::Scalar, const D: usize> Schema |
| 45 | + for nalgebra_v0_34::Translation<T, D> |
| 46 | +{ |
| 47 | + const SCHEMA: &'static DataModelType = nalgebra_v0_34::SVector::<T, D>::SCHEMA; |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg_attr(docsrs, doc(cfg(feature = "nalgebra-v0_34")))] |
| 51 | +impl<T: Schema + nalgebra_v0_34::Scalar, R: Schema, const D: usize> Schema |
| 52 | + for nalgebra_v0_34::Isometry<T, R, D> |
| 53 | +{ |
| 54 | + const SCHEMA: &'static DataModelType = &DataModelType::Struct { |
| 55 | + name: "Isometry", |
| 56 | + data: Data::Struct(&[ |
| 57 | + &NamedField { |
| 58 | + name: "rotation", |
| 59 | + ty: R::SCHEMA, |
| 60 | + }, |
| 61 | + &NamedField { |
| 62 | + name: "translation", |
| 63 | + ty: nalgebra_v0_34::Translation::<T, D>::SCHEMA, |
| 64 | + }, |
| 65 | + ]), |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +/// Const version of the const-unstable [`<[[T; N]]>::as_flattened()`] |
| 70 | +const fn flatten<T, const N: usize>(slice: &[[T; N]]) -> &[T] { |
| 71 | + const { |
| 72 | + assert!(size_of::<T>() != 0); |
| 73 | + } |
| 74 | + // SAFETY: `self.len() * N` cannot overflow because `self` is |
| 75 | + // already in the address space. |
| 76 | + let len = unsafe { slice.len().unchecked_mul(N) }; |
| 77 | + // SAFETY: `[T]` is layout-identical to `[T; N]` |
| 78 | + unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), len) } |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn flattened() { |
| 83 | + type T = nalgebra_v0_34::SMatrix<u8, 3, 3>; |
| 84 | + assert_eq!(T::SCHEMA, <[u8; 9]>::SCHEMA); |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn smoke() { |
| 89 | + let x = nalgebra_v0_34::SMatrix::<u8, 3, 3>::new(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 90 | + let y = postcard::to_stdvec(&x).unwrap(); |
| 91 | + assert_eq!(&[1, 4, 7, 2, 5, 8, 3, 6, 9], y.as_slice()); |
| 92 | +} |
0 commit comments