add bytecheck for matrix

This commit is contained in:
zyansheep 2022-03-21 12:47:03 -04:00 committed by Saurabh
parent 8c36ab4ceb
commit 7b5fb956d0
2 changed files with 19 additions and 1 deletions

View File

@ -52,7 +52,7 @@ convert-glam020 = [ "glam020" ]
## `serde-serialize`. ## `serde-serialize`.
serde-serialize-no-std = [ "serde", "num-complex/serde" ] serde-serialize-no-std = [ "serde", "num-complex/serde" ]
serde-serialize = [ "serde-serialize-no-std", "serde/std" ] serde-serialize = [ "serde-serialize-no-std", "serde/std" ]
rkyv-serialize-no-std = [ "rkyv" ] rkyv-serialize-no-std = [ "rkyv", "bytecheck" ]
rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std" ] rkyv-serialize = [ "rkyv-serialize-no-std", "rkyv/std" ]
# Randomness # Randomness
@ -80,6 +80,7 @@ rand_distr = { version = "0.4", default-features = false, optional = true }
matrixmultiply = { version = "0.3", optional = true } matrixmultiply = { version = "0.3", optional = true }
serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true } serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true }
rkyv = { version = "~0.7.1", optional = true } rkyv = { version = "~0.7.1", optional = true }
bytecheck = { version = "~0.6.1", optional = true }
mint = { version = "0.5", optional = true } mint = { version = "0.5", optional = true }
quickcheck = { version = "1", optional = true } quickcheck = { version = "1", optional = true }
pest = { version = "2", optional = true } pest = { version = "2", optional = true }

View File

@ -293,6 +293,7 @@ mod rkyv_impl {
use super::Matrix; use super::Matrix;
use core::marker::PhantomData; use core::marker::PhantomData;
use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize}; use rkyv::{out_field, Archive, Deserialize, Fallible, Serialize};
use bytecheck::CheckBytes;
impl<T: Archive, R: Archive, C: Archive, S: Archive> Archive for Matrix<T, R, C, S> { impl<T: Archive, R: Archive, C: Archive, S: Archive> Archive for Matrix<T, R, C, S> {
type Archived = Matrix<T::Archived, R::Archived, C::Archived, S::Archived>; type Archived = Matrix<T::Archived, R::Archived, C::Archived, S::Archived>;
@ -325,6 +326,22 @@ mod rkyv_impl {
}) })
} }
} }
impl<__C: ?Sized, T, R, C, S: CheckBytes<__C>>
CheckBytes<__C>
for Matrix<T, R, C, S>
where
S: CheckBytes<__C>,
{
type Error = <S as CheckBytes<__C>>::Error;
unsafe fn check_bytes<'a>(
value: *const Matrix<T, R, C, S>,
context: &mut __C,
) -> Result<&'a Self, Self::Error> {
let _ = S::check_bytes(::core::ptr::addr_of!((*value).data), context)?;
Ok(&*value)
}
}
} }
impl<T, R, C, S> Matrix<T, R, C, S> { impl<T, R, C, S> Matrix<T, R, C, S> {