Skip to content

Commit 4e38494

Browse files
authored
Merge pull request #553 from GnomedDev/sorted-linked-list-lentype
Migrate SortedLinkedList to LenType
2 parents ad5a0ee + 94a9eef commit 4e38494

File tree

3 files changed

+124
-167
lines changed

3 files changed

+124
-167
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5252
- Added `LenT` generic to `Vec<T, N>` and `VecView<T>` to save memory when using a sane capacity value.
5353
- Added the `index_set` module.
5454
- Added the `index_map` module.
55+
- Migrated `Idx` generic for `SortedLinkedList` to use the new `LenType` trait, allowing for `Idx` inference.
5556

5657
### Changed
5758

src/len_type.rs

Lines changed: 16 additions & 3 deletions
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 to_non_max(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)