Coverage Report

Created: 2026-01-13 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/nalgebra-0.34.1/src/base/dimension.rs
Line
Count
Source
1
#![allow(missing_docs)]
2
3
//! Traits and tags for identifying the dimension of all algebraic entities.
4
5
use std::any::{Any, TypeId};
6
use std::cmp;
7
use std::fmt::Debug;
8
use std::ops::{Add, Div, Mul, Sub};
9
use typenum::{self, Diff, Max, Maximum, Min, Minimum, Prod, Quot, Sum, Unsigned};
10
11
#[cfg(feature = "rkyv-serialize")]
12
use rkyv::bytecheck;
13
#[cfg(feature = "serde-serialize-no-std")]
14
use serde::{Deserialize, Deserializer, Serialize, Serializer};
15
16
/// Dim of dynamically-sized algebraic entities.
17
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
18
#[cfg_attr(
19
    feature = "rkyv-serialize-no-std",
20
    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
21
)]
22
#[cfg_attr(
23
    feature = "rkyv-serialize",
24
    archive_attr(derive(bytecheck::CheckBytes))
25
)]
26
pub struct Dyn(pub usize);
27
28
#[deprecated(note = "use Dyn instead.")]
29
pub type Dynamic = Dyn;
30
31
impl Dyn {
32
    /// A dynamic size equal to `value`.
33
    #[inline]
34
    #[deprecated(note = "use Dyn(value) instead.")]
35
    pub const fn new(value: usize) -> Self {
36
        Self(value)
37
    }
38
}
39
40
#[cfg(feature = "serde-serialize-no-std")]
41
impl Serialize for Dyn {
42
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43
    where
44
        S: Serializer,
45
    {
46
        self.0.serialize(serializer)
47
    }
48
}
49
50
#[cfg(feature = "serde-serialize-no-std")]
51
impl<'de> Deserialize<'de> for Dyn {
52
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
53
    where
54
        D: Deserializer<'de>,
55
    {
56
        usize::deserialize(deserializer).map(Dyn)
57
    }
58
}
59
60
/// Trait implemented by `Dyn`.
61
pub trait IsDynamic {}
62
/// Trait implemented by `Dyn` and type-level integers different from `U1`.
63
pub trait IsNotStaticOne {}
64
65
impl IsDynamic for Dyn {}
66
impl IsNotStaticOne for Dyn {}
67
68
/// Trait implemented by any type that can be used as a dimension. This includes type-level
69
/// integers and `Dyn` (for dimensions not known at compile-time).
70
///
71
/// # Safety
72
///
73
/// Hoists integers to the type level, including binary operations.
74
pub unsafe trait Dim: Any + Debug + Copy + PartialEq + Send + Sync {
75
    #[inline(always)]
76
3.61k
    fn is<D: Dim>() -> bool {
77
3.61k
        TypeId::of::<Self>() == TypeId::of::<D>()
78
3.61k
    }
<nalgebra::base::dimension::Const<1> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<1>>
Line
Count
Source
76
486
    fn is<D: Dim>() -> bool {
77
486
        TypeId::of::<Self>() == TypeId::of::<D>()
78
486
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<1> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Dyn>
Unexecuted instantiation: <nalgebra::base::dimension::Const<2> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<2>>
Unexecuted instantiation: <nalgebra::base::dimension::Const<2> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<3>>
Unexecuted instantiation: <nalgebra::base::dimension::Const<2> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<4>>
Unexecuted instantiation: <nalgebra::base::dimension::Const<2> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Dyn>
<nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<3>>
Line
Count
Source
76
486
    fn is<D: Dim>() -> bool {
77
486
        TypeId::of::<Self>() == TypeId::of::<D>()
78
486
    }
<nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<2>>
Line
Count
Source
76
972
    fn is<D: Dim>() -> bool {
77
972
        TypeId::of::<Self>() == TypeId::of::<D>()
78
972
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<4>>
<nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Dyn>
Line
Count
Source
76
1.67k
    fn is<D: Dim>() -> bool {
77
1.67k
        TypeId::of::<Self>() == TypeId::of::<D>()
78
1.67k
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<4> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Dyn>
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<2>>
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<3>>
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<4>>
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Dyn>
Unexecuted instantiation: <nalgebra::base::dimension::Dyn as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Dyn>
Unexecuted instantiation: <nalgebra::base::dimension::Dyn as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<2>>
Unexecuted instantiation: <nalgebra::base::dimension::Dyn as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<3>>
Unexecuted instantiation: <nalgebra::base::dimension::Dyn as nalgebra::base::dimension::Dim>::is::<nalgebra::base::dimension::Const<4>>
79
80
    /// Gets the compile-time value of `Self`. Returns `None` if it is not known, i.e., if `Self =
81
    /// Dyn`.
82
    fn try_to_usize() -> Option<usize>;
83
84
    /// Gets the run-time value of `self`. For type-level integers, this is the same as
85
    /// `Self::try_to_usize().unwrap()`.
86
    fn value(&self) -> usize;
87
88
    /// Builds an instance of `Self` from a run-time value. Panics if `Self` is a type-level
89
    /// integer and `dim != Self::try_to_usize().unwrap()`.
90
    fn from_usize(dim: usize) -> Self;
91
}
92
93
unsafe impl Dim for Dyn {
94
    #[inline]
95
    fn try_to_usize() -> Option<usize> {
96
        None
97
    }
98
99
    #[inline]
100
    fn from_usize(dim: usize) -> Self {
101
        Self(dim)
102
    }
103
104
    #[inline]
105
0
    fn value(&self) -> usize {
106
0
        self.0
107
0
    }
108
}
109
110
impl Add<usize> for Dyn {
111
    type Output = Dyn;
112
113
    #[inline]
114
    fn add(self, rhs: usize) -> Self {
115
        Self(self.0 + rhs)
116
    }
117
}
118
119
impl Sub<usize> for Dyn {
120
    type Output = Dyn;
121
122
    #[inline]
123
    fn sub(self, rhs: usize) -> Self {
124
        Self(self.0 - rhs)
125
    }
126
}
127
128
/*
129
 *
130
 * Operations.
131
 *
132
 */
133
134
macro_rules! dim_ops(
135
    ($($DimOp:    ident, $DimNameOp: ident,
136
       $Op:       ident, $op: ident, $op_path: path,
137
       $DimResOp: ident, $DimNameResOp: ident,
138
       $ResOp: ident);* $(;)*) => {$(
139
        pub type $DimResOp<D1, D2> = <D1 as $DimOp<D2>>::Output;
140
141
        pub trait $DimOp<D: Dim>: Dim {
142
            type Output: Dim;
143
144
            fn $op(self, other: D) -> Self::Output;
145
        }
146
147
        impl<const A: usize, const B: usize> $DimOp<Const<B>> for Const<A>
148
        where
149
            Const<A>: ToTypenum,
150
            Const<B>: ToTypenum,
151
            <Const<A> as ToTypenum>::Typenum: $Op<<Const<B> as ToTypenum>::Typenum>,
152
            $ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum>: ToConst,
153
        {
154
            type Output =
155
                <$ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum> as ToConst>::Const;
156
157
0
            fn $op(self, _: Const<B>) -> Self::Output {
158
0
                Self::Output::name()
159
0
            }
Unexecuted instantiation: <nalgebra::base::dimension::Const<2> as nalgebra::base::dimension::DimSub<nalgebra::base::dimension::Const<1>>>::sub
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::DimSub<nalgebra::base::dimension::Const<1>>>::sub
Unexecuted instantiation: <nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::DimMin<nalgebra::base::dimension::Const<3>>>::min
160
        }
161
162
        impl<D: Dim> $DimOp<D> for Dyn {
163
            type Output = Dyn;
164
165
            #[inline]
166
            fn $op(self, other: D) -> Dyn {
167
                Dyn($op_path(self.value(), other.value()))
168
            }
169
        }
170
171
        // TODO: use Const<T> instead of D: DimName?
172
        impl<D: DimName> $DimOp<Dyn> for D {
173
            type Output = Dyn;
174
175
            #[inline]
176
            fn $op(self, other: Dyn) -> Dyn {
177
                Dyn($op_path(self.value(), other.value()))
178
            }
179
        }
180
181
        pub type $DimNameResOp<D1, D2> = <D1 as $DimNameOp<D2>>::Output;
182
183
        pub trait $DimNameOp<D: DimName>: DimName {
184
            type Output: DimName;
185
186
            fn $op(self, other: D) -> Self::Output;
187
        }
188
189
        impl<const A: usize, const B: usize> $DimNameOp<Const<B>> for Const<A>
190
        where
191
            Const<A>: ToTypenum,
192
            Const<B>: ToTypenum,
193
            <Const<A> as ToTypenum>::Typenum: $Op<<Const<B> as ToTypenum>::Typenum>,
194
            $ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum>: ToConst,
195
        {
196
            type Output =
197
                <$ResOp<<Const<A> as ToTypenum>::Typenum, <Const<B> as ToTypenum>::Typenum> as ToConst>::Const;
198
199
            fn $op(self, _: Const<B>) -> Self::Output {
200
                Self::Output::name()
201
            }
202
        }
203
   )*}
204
);
205
206
dim_ops!(
207
    DimAdd, DimNameAdd, Add, add, Add::add, DimSum,     DimNameSum,     Sum;
208
    DimMul, DimNameMul, Mul, mul, Mul::mul, DimProd,    DimNameProd,    Prod;
209
    DimSub, DimNameSub, Sub, sub, Sub::sub, DimDiff,    DimNameDiff,    Diff;
210
    DimDiv, DimNameDiv, Div, div, Div::div, DimQuot,    DimNameQuot,    Quot;
211
    DimMin, DimNameMin, Min, min, cmp::min, DimMinimum, DimNameMinimum, Minimum;
212
    DimMax, DimNameMax, Max, max, cmp::max, DimMaximum, DimNameMaximum, Maximum;
213
);
214
215
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
216
#[cfg_attr(
217
    feature = "rkyv-serialize-no-std",
218
    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
219
    archive(as = "Self")
220
)]
221
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
222
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
223
pub struct Const<const R: usize>;
224
225
/// Trait implemented exclusively by type-level integers.
226
pub trait DimName: Dim {
227
    const DIM: usize;
228
229
    /// The name of this dimension, i.e., the singleton `Self`.
230
    fn name() -> Self;
231
232
    // TODO: this is not a very idiomatic name.
233
    /// The value of this dimension.
234
    fn dim() -> usize;
235
}
236
237
#[cfg(feature = "serde-serialize-no-std")]
238
impl<const D: usize> Serialize for Const<D> {
239
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
240
    where
241
        S: Serializer,
242
    {
243
        ().serialize(serializer)
244
    }
245
}
246
247
#[cfg(feature = "serde-serialize-no-std")]
248
impl<'de, const D: usize> Deserialize<'de> for Const<D> {
249
    fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
250
    where
251
        Des: Deserializer<'de>,
252
    {
253
        <()>::deserialize(deserializer).map(|_| Const::<D>)
254
    }
255
}
256
257
pub trait ToConst {
258
    type Const: DimName;
259
}
260
261
pub trait ToTypenum {
262
    type Typenum: Unsigned;
263
}
264
265
unsafe impl<const T: usize> Dim for Const<T> {
266
    #[inline]
267
0
    fn try_to_usize() -> Option<usize> {
268
0
        Some(T)
269
0
    }
270
271
    #[inline]
272
606k
    fn value(&self) -> usize {
273
606k
        T
274
606k
    }
<nalgebra::base::dimension::Const<1> as nalgebra::base::dimension::Dim>::value
Line
Count
Source
272
208k
    fn value(&self) -> usize {
273
208k
        T
274
208k
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<2> as nalgebra::base::dimension::Dim>::value
<nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::Dim>::value
Line
Count
Source
272
334k
    fn value(&self) -> usize {
273
334k
        T
274
334k
    }
<nalgebra::base::dimension::Const<4> as nalgebra::base::dimension::Dim>::value
Line
Count
Source
272
63.4k
    fn value(&self) -> usize {
273
63.4k
        T
274
63.4k
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<5> as nalgebra::base::dimension::Dim>::value
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::Dim>::value
Unexecuted instantiation: <nalgebra::base::dimension::Const<7> as nalgebra::base::dimension::Dim>::value
275
276
    #[inline]
277
356
    fn from_usize(dim: usize) -> Self {
278
356
        assert_eq!(dim, T);
279
356
        Self
280
356
    }
<nalgebra::base::dimension::Const<1> as nalgebra::base::dimension::Dim>::from_usize
Line
Count
Source
277
178
    fn from_usize(dim: usize) -> Self {
278
178
        assert_eq!(dim, T);
279
178
        Self
280
178
    }
<nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::Dim>::from_usize
Line
Count
Source
277
178
    fn from_usize(dim: usize) -> Self {
278
178
        assert_eq!(dim, T);
279
178
        Self
280
178
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::Dim>::from_usize
281
}
282
283
impl<const T: usize> DimName for Const<T> {
284
    const DIM: usize = T;
285
286
    #[inline]
287
9.51k
    fn name() -> Self {
288
9.51k
        Self
289
9.51k
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<1> as nalgebra::base::dimension::DimName>::name
<nalgebra::base::dimension::Const<3> as nalgebra::base::dimension::DimName>::name
Line
Count
Source
287
9.51k
    fn name() -> Self {
288
9.51k
        Self
289
9.51k
    }
Unexecuted instantiation: <nalgebra::base::dimension::Const<5> as nalgebra::base::dimension::DimName>::name
Unexecuted instantiation: <nalgebra::base::dimension::Const<6> as nalgebra::base::dimension::DimName>::name
Unexecuted instantiation: <nalgebra::base::dimension::Const<7> as nalgebra::base::dimension::DimName>::name
290
291
    #[inline]
292
    fn dim() -> usize {
293
        T
294
    }
295
}
296
297
pub type U1 = Const<1>;
298
299
impl ToTypenum for Const<1> {
300
    type Typenum = typenum::U1;
301
}
302
303
impl ToConst for typenum::U1 {
304
    type Const = Const<1>;
305
}
306
307
macro_rules! from_to_typenum (
308
    ($($D: ident, $VAL: expr_2021);* $(;)*) => {$(
309
        pub type $D = Const<$VAL>;
310
311
        impl ToTypenum for Const<$VAL> {
312
            type Typenum = typenum::$D;
313
        }
314
315
        impl ToConst for typenum::$D {
316
            type Const = Const<$VAL>;
317
        }
318
319
        impl IsNotStaticOne for $D { }
320
321
        /// The constant dimension
322
        #[doc = stringify!($VAL)]
323
        /// .
324
        pub const $D: $D = Const::<$VAL>;
325
    )*}
326
);
327
328
from_to_typenum!(
329
    U0, 0; /*U1,1;*/ U2, 2; U3, 3; U4, 4; U5, 5; U6, 6; U7, 7; U8, 8; U9, 9; U10, 10; U11, 11; U12, 12; U13, 13; U14, 14; U15, 15; U16, 16; U17, 17; U18, 18;
330
    U19, 19; U20, 20; U21, 21; U22, 22; U23, 23; U24, 24; U25, 25; U26, 26; U27, 27; U28, 28; U29, 29; U30, 30; U31, 31; U32, 32; U33, 33; U34, 34; U35, 35; U36, 36; U37, 37;
331
    U38, 38; U39, 39; U40, 40; U41, 41; U42, 42; U43, 43; U44, 44; U45, 45; U46, 46; U47, 47; U48, 48; U49, 49; U50, 50; U51, 51; U52, 52; U53, 53; U54, 54; U55, 55; U56, 56;
332
    U57, 57; U58, 58; U59, 59; U60, 60; U61, 61; U62, 62; U63, 63; U64, 64; U65, 65; U66, 66; U67, 67; U68, 68; U69, 69; U70, 70; U71, 71; U72, 72; U73, 73; U74, 74; U75, 75;
333
    U76, 76; U77, 77; U78, 78; U79, 79; U80, 80; U81, 81; U82, 82; U83, 83; U84, 84; U85, 85; U86, 86; U87, 87; U88, 88; U89, 89; U90, 90; U91, 91; U92, 92; U93, 93; U94, 94;
334
    U95, 95; U96, 96; U97, 97; U98, 98; U99, 99; U100, 100; U101, 101; U102, 102; U103, 103; U104, 104; U105, 105; U106, 106; U107, 107; U108, 108; U109, 109; U110, 110;
335
    U111, 111; U112, 112; U113, 113; U114, 114; U115, 115; U116, 116; U117, 117; U118, 118; U119, 119; U120, 120; U121, 121; U122, 122; U123, 123; U124, 124; U125, 125; U126, 126;
336
    U127, 127
337
);
338
339
/// The constant dimension 1.
340
// Note: We add U1 separately since it's not covered by the from_to_typenum! macro.
341
pub const U1: U1 = Const::<1>;