Implement SMatrix::from_array_storage and use it in matriX! impl

This allows us to avoid injecting unsafe code into every macro
invocation, which seems desirable.
This commit is contained in:
Andreas Longva 2021-04-17 16:41:52 +02:00
parent 7098a4f07e
commit 9142dc8f84
2 changed files with 13 additions and 4 deletions

View File

@ -90,12 +90,9 @@ pub fn matrix(stream: TokenStream) -> TokenStream {
let array_tokens = matrix.col_major_array_tokens();
// TODO: Use quote_spanned instead??
// TODO: Avoid use of unsafe here
let output = quote! {
unsafe {
nalgebra::SMatrix::<_, #row_dim, #col_dim>
::from_data_statically_unchecked(nalgebra::ArrayStorage(#array_tokens))
}
::from_array_storage(nalgebra::ArrayStorage(#array_tokens))
};
proc_macro::TokenStream::from(output)

View File

@ -25,6 +25,7 @@ use crate::base::storage::{
ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut,
};
use crate::base::Scalar;
use crate::SMatrix;
/*
*
@ -299,3 +300,14 @@ where
self.as_slice().iter().fold(0, |acc, e| acc + e.extent())
}
}
// TODO: Where to put this impl block?
impl<T, const R: usize, const C: usize> SMatrix<T, R, C> {
/// Creates a new statically-allocated matrix from the given [ArrayStorage].
#[inline(always)]
pub const fn from_array_storage(storage: ArrayStorage<T, R, C>) -> Self {
// This is sound because the row and column types are exactly the same as that of the
// storage, so there can be no mismatch
unsafe { Self::from_data_statically_unchecked(storage) }
}
}