@@ -22,8 +22,10 @@ pub trait Sealed:
22
22
const ZERO : Self ;
23
23
/// The one value of the integer type.
24
24
const ONE : Self ;
25
+ /// The maximum value of this type.
26
+ const MAX : Self ;
25
27
/// The maximum value of this type, as a `usize`.
26
- const MAX : usize ;
28
+ const MAX_USIZE : usize ;
27
29
28
30
/// An infallible conversion from `usize` to `LenT`.
29
31
#[ inline]
@@ -36,6 +38,16 @@ pub trait Sealed:
36
38
fn into_usize ( self ) -> usize {
37
39
self . try_into ( ) . unwrap ( )
38
40
}
41
+
42
+ /// Converts `LenT` into `Some(usize)`, unless it's `Self::MAX`, where it returns `None`.
43
+ #[ inline]
44
+ fn option ( self ) -> Option < usize > {
45
+ if self == Self :: MAX {
46
+ None
47
+ } else {
48
+ Some ( self . into_usize ( ) )
49
+ }
50
+ }
39
51
}
40
52
41
53
macro_rules! impl_lentype {
@@ -44,7 +56,8 @@ macro_rules! impl_lentype {
44
56
impl Sealed for $LenT {
45
57
const ZERO : Self = 0 ;
46
58
const ONE : Self = 1 ;
47
- const MAX : usize = Self :: MAX as _;
59
+ const MAX : Self = Self :: MAX ;
60
+ const MAX_USIZE : usize = Self :: MAX as _;
48
61
}
49
62
50
63
$( #[ $meta] ) *
@@ -103,5 +116,5 @@ impl_lentodefault!(u16: 256, 300, 400, 500, 512, 600, 700, 800, 900, 1000, 1024,
103
116
impl_lentodefault ! ( u32 : 65536 , 131072 , 262144 , 524288 , 1048576 , 2097152 , 4194304 , 8388608 , 16777216 , 33554432 , 67108864 , 134217728 , 268435456 , 536870912 , 1073741824 , 2147483648 ) ;
104
117
105
118
pub const fn check_capacity_fits < LenT : LenType , const N : usize > ( ) {
106
- assert ! ( LenT :: MAX >= N , "The capacity is larger than `LenT` can hold, increase the size of `LenT` or reduce the capacity" ) ;
119
+ assert ! ( LenT :: MAX_USIZE >= N , "The capacity is larger than `LenT` can hold, increase the size of `LenT` or reduce the capacity" ) ;
107
120
}
0 commit comments