forked from M-Labs/nalgebra
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:
parent
7098a4f07e
commit
9142dc8f84
@ -90,12 +90,9 @@ pub fn matrix(stream: TokenStream) -> TokenStream {
|
|||||||
let array_tokens = matrix.col_major_array_tokens();
|
let array_tokens = matrix.col_major_array_tokens();
|
||||||
|
|
||||||
// TODO: Use quote_spanned instead??
|
// TODO: Use quote_spanned instead??
|
||||||
// TODO: Avoid use of unsafe here
|
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
unsafe {
|
|
||||||
nalgebra::SMatrix::<_, #row_dim, #col_dim>
|
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)
|
proc_macro::TokenStream::from(output)
|
||||||
|
@ -25,6 +25,7 @@ use crate::base::storage::{
|
|||||||
ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut,
|
ContiguousStorage, ContiguousStorageMut, Owned, ReshapableStorage, Storage, StorageMut,
|
||||||
};
|
};
|
||||||
use crate::base::Scalar;
|
use crate::base::Scalar;
|
||||||
|
use crate::SMatrix;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
@ -299,3 +300,14 @@ where
|
|||||||
self.as_slice().iter().fold(0, |acc, e| acc + e.extent())
|
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) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user