Skip to content

Commit ba30c9d

Browse files
TimTheBigsebcrozet
andauthored
Improve compile-time usability (#1522)
* Add `shape_const` to fixed size matrices * Add const to all possible fns * Update src/base/matrix.rs * chore: cargo fmt + changelog * chore: bump MSRV to 1.87 (for const Vec::len). * chore: update changelog --------- Co-authored-by: Sébastien Crozet <sebcrozet@dimforge.com>
1 parent 2b0affb commit ba30c9d

38 files changed

Lines changed: 87 additions & 94 deletions

.github/workflows/nalgebra-ci-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
- name: Select rustc version
6161
uses: actions-rs/toolchain@v1
6262
with:
63-
toolchain: 1.85.0
63+
toolchain: 1.87.0
6464
override: true
6565
- uses: actions/checkout@v4
6666
- name: check
@@ -77,7 +77,7 @@ jobs:
7777
- name: Select rustc version
7878
uses: actions-rs/toolchain@v1
7979
with:
80-
toolchain: 1.85.0
80+
toolchain: 1.87.0
8181
override: true
8282
- uses: actions/checkout@v4
8383
- name: test

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1313

1414
### Changed
1515

16+
- Bumped MSRV to 1.87.0.
17+
- Renamed associated const `DimName::USIZE` to `DimName::DIM`.
1618
- Moved to Rust 2024 edition.
19+
- Several methods are now `const` whenever possible. See details in [#1522](https://github.com/dimforge/nalgebra/pull/1522).
1720

1821
## [0.33.2] (29 October 2024)
1922

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories = ["science", "mathematics", "wasm", "no-std"]
1212
keywords = ["linear", "algebra", "matrix", "vector", "math"]
1313
license = "Apache-2.0"
1414
edition = "2024"
15-
rust-version = "1.85.0"
15+
rust-version = "1.87.0"
1616
exclude = ["/ci/*", "/.travis.yml", "/Makefile"]
1717

1818
[badges]

src/base/cg.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ where
3131
#[inline]
3232
pub fn new_scaling(scaling: T) -> Self {
3333
let mut res = Self::from_diagonal_element(scaling);
34-
res[(D::dim() - 1, D::dim() - 1)] = T::one();
34+
res[(D::DIM - 1, D::DIM - 1)] = T::one();
3535

3636
res
3737
}
@@ -59,11 +59,8 @@ where
5959
SB: Storage<T, DimNameDiff<D, U1>>,
6060
{
6161
let mut res = Self::identity();
62-
res.generic_view_mut(
63-
(0, D::dim() - 1),
64-
(DimNameDiff::<D, U1>::name(), Const::<1>),
65-
)
66-
.copy_from(translation);
62+
res.generic_view_mut((0, D::DIM - 1), (DimNameDiff::<D, U1>::name(), Const::<1>))
63+
.copy_from(translation);
6764

6865
res
6966
}
@@ -364,9 +361,9 @@ impl<T: Scalar + Zero + One + ClosedMulAssign + ClosedAddAssign, D: DimName, S:
364361
D: DimNameSub<U1>,
365362
SB: Storage<T, DimNameDiff<D, U1>>,
366363
{
367-
for i in 0..D::dim() {
368-
for j in 0..D::dim() - 1 {
369-
let add = shift[j].clone() * self[(D::dim() - 1, i)].clone();
364+
for i in 0..D::DIM {
365+
for j in 0..D::DIM - 1 {
366+
let add = shift[j].clone() * self[(D::DIM - 1, i)].clone();
370367
self[(j, i)] += add;
371368
}
372369
}
@@ -382,22 +379,17 @@ impl<T: Scalar + Zero + One + ClosedMulAssign + ClosedAddAssign, D: DimName, S:
382379
DefaultAllocator: Allocator<DimNameDiff<D, U1>>,
383380
{
384381
let scale = self
385-
.generic_view(
386-
(D::dim() - 1, 0),
387-
(Const::<1>, DimNameDiff::<D, U1>::name()),
388-
)
382+
.generic_view((D::DIM - 1, 0), (Const::<1>, DimNameDiff::<D, U1>::name()))
389383
.tr_dot(shift);
390384
let post_translation = self.generic_view(
391385
(0, 0),
392386
(DimNameDiff::<D, U1>::name(), DimNameDiff::<D, U1>::name()),
393387
) * shift;
394388

395-
self[(D::dim() - 1, D::dim() - 1)] += scale;
389+
self[(D::DIM - 1, D::DIM - 1)] += scale;
396390

397-
let mut translation = self.generic_view_mut(
398-
(0, D::dim() - 1),
399-
(DimNameDiff::<D, U1>::name(), Const::<1>),
400-
);
391+
let mut translation =
392+
self.generic_view_mut((0, D::DIM - 1), (DimNameDiff::<D, U1>::name(), Const::<1>));
401393
translation += post_translation;
402394
}
403395
}
@@ -419,10 +411,8 @@ where
419411
(0, 0),
420412
(DimNameDiff::<D, U1>::name(), DimNameDiff::<D, U1>::name()),
421413
);
422-
let normalizer = self.generic_view(
423-
(D::dim() - 1, 0),
424-
(Const::<1>, DimNameDiff::<D, U1>::name()),
425-
);
414+
let normalizer =
415+
self.generic_view((D::DIM - 1, 0), (Const::<1>, DimNameDiff::<D, U1>::name()));
426416
let n = normalizer.tr_dot(v);
427417

428418
if !n.is_zero() {

src/base/construction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,12 @@ R::name(), C::name(); // Arguments for `_generic` constructors.
792792

793793
impl_constructors_from_data!(data; R, Dyn;
794794
=> R: DimName;
795-
R::name(), Dyn(data.len() / R::dim());
795+
R::name(), Dyn(data.len() / R::DIM);
796796
);
797797

798798
impl_constructors_from_data!(data; Dyn, C;
799799
=> C: DimName;
800-
Dyn(data.len() / C::dim()), C::name();
800+
Dyn(data.len() / C::DIM), C::name();
801801
);
802802

803803
#[cfg(any(feature = "std", feature = "alloc"))]

src/base/construction_view.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim>
1414
/// This method is unsafe because the input data array is not checked to contain enough elements.
1515
/// The generic types `R`, `C`, `RStride`, `CStride` can either be type-level integers or integers wrapped with `Dyn()`.
1616
#[inline]
17-
pub unsafe fn from_slice_with_strides_generic_unchecked(
17+
pub const unsafe fn from_slice_with_strides_generic_unchecked(
1818
data: &'a [T],
1919
start: usize,
2020
nrows: R,
@@ -163,7 +163,7 @@ impl<'a, T: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim>
163163
/// This method is unsafe because the input data array is not checked to contain enough elements.
164164
/// The generic types `R`, `C`, `RStride`, `CStride` can either be type-level integers or integers wrapped with `Dyn()`.
165165
#[inline]
166-
pub unsafe fn from_slice_with_strides_generic_unchecked(
166+
pub const unsafe fn from_slice_with_strides_generic_unchecked(
167167
data: &'a mut [T],
168168
start: usize,
169169
nrows: R,

src/base/dimension.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ pub struct Const<const R: usize>;
223223

224224
/// Trait implemented exclusively by type-level integers.
225225
pub trait DimName: Dim {
226-
const USIZE: usize;
226+
const DIM: usize;
227227

228228
/// The name of this dimension, i.e., the singleton `Self`.
229229
fn name() -> Self;
@@ -280,7 +280,7 @@ unsafe impl<const T: usize> Dim for Const<T> {
280280
}
281281

282282
impl<const T: usize> DimName for Const<T> {
283-
const USIZE: usize = T;
283+
const DIM: usize = T;
284284

285285
#[inline]
286286
fn name() -> Self {

src/base/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub struct RowIter<'a, T, R: Dim, C: Dim, S: RawStorage<T, R, C>> {
279279
}
280280

281281
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> RowIter<'a, T, R, C, S> {
282-
pub(crate) fn new(mat: &'a Matrix<T, R, C, S>) -> Self {
282+
pub(crate) const fn new(mat: &'a Matrix<T, R, C, S>) -> Self {
283283
RowIter { mat, curr: 0 }
284284
}
285285
}
@@ -330,7 +330,7 @@ pub struct RowIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> {
330330
}
331331

332332
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> RowIterMut<'a, T, R, C, S> {
333-
pub(crate) fn new(mat: &'a mut Matrix<T, R, C, S>) -> Self {
333+
pub(crate) const fn new(mat: &'a mut Matrix<T, R, C, S>) -> Self {
334334
RowIterMut {
335335
mat,
336336
curr: 0,

src/base/matrix.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ where
408408
impl<T, R: Dim, C: Dim, S: RawStorage<T, R, C>> Matrix<T, R, C, S> {
409409
/// Creates a new matrix with the given data.
410410
#[inline(always)]
411-
pub fn from_data(data: S) -> Self {
411+
pub const fn from_data(data: S) -> Self {
412412
unsafe { Self::from_data_statically_unchecked(data) }
413413
}
414414

@@ -1115,7 +1115,7 @@ impl<T, R: Dim, C: Dim, S: RawStorage<T, R, C>> Matrix<T, R, C, S> {
11151115
/// }
11161116
/// ```
11171117
#[inline]
1118-
pub fn row_iter(&self) -> RowIter<'_, T, R, C, S> {
1118+
pub const fn row_iter(&self) -> RowIter<'_, T, R, C, S> {
11191119
RowIter::new(self)
11201120
}
11211121

@@ -1160,7 +1160,7 @@ impl<T, R: Dim, C: Dim, S: RawStorage<T, R, C>> Matrix<T, R, C, S> {
11601160
/// assert_eq!(a, expected);
11611161
/// ```
11621162
#[inline]
1163-
pub fn row_iter_mut(&mut self) -> RowIterMut<'_, T, R, C, S>
1163+
pub const fn row_iter_mut(&mut self) -> RowIterMut<'_, T, R, C, S>
11641164
where
11651165
S: RawStorageMut<T, R, C>,
11661166
{

src/base/matrix_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ macro_rules! view_storage_impl (
4848
///
4949
/// `*ptr` must point to memory that is valid `[T; R * C]`.
5050
#[inline]
51-
pub unsafe fn from_raw_parts(ptr: $Ptr,
51+
pub const unsafe fn from_raw_parts(ptr: $Ptr,
5252
shape: (R, C),
5353
strides: (RStride, CStride))
5454
-> Self

0 commit comments

Comments
 (0)