Skip to content

Commit 2d38c73

Browse files
committed
Migrate SortedLinkedList to LenType
1 parent fb62d12 commit 2d38c73

File tree

2 files changed

+110
-154
lines changed

2 files changed

+110
-154
lines changed

src/len_type.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ pub trait Sealed:
2222
const ZERO: Self;
2323
/// The one value of the integer type.
2424
const ONE: Self;
25+
/// The maximum value of this type.
26+
const MAX: Self;
2527
/// The maximum value of this type, as a `usize`.
26-
const MAX: usize;
28+
const MAX_USIZE: usize;
2729

2830
/// An infallible conversion from `usize` to `LenT`.
2931
#[inline]
@@ -36,6 +38,16 @@ pub trait Sealed:
3638
fn into_usize(self) -> usize {
3739
self.try_into().unwrap()
3840
}
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+
}
3951
}
4052

4153
macro_rules! impl_lentype {
@@ -44,7 +56,8 @@ macro_rules! impl_lentype {
4456
impl Sealed for $LenT {
4557
const ZERO: Self = 0;
4658
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 _;
4861
}
4962

5063
$(#[$meta])*
@@ -103,5 +116,5 @@ impl_lentodefault!(u16: 256, 300, 400, 500, 512, 600, 700, 800, 900, 1000, 1024,
103116
impl_lentodefault!(u32: 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648);
104117

105118
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");
107120
}

0 commit comments

Comments
 (0)