diff --git a/Cargo.toml b/Cargo.toml index 6e64370..b53980a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ringmap" edition = "2021" -version = "0.2.3" +version = "0.2.4" documentation = "https://docs.rs/ringmap/" repository = "https://github.com/indexmap-rs/ringmap" license = "Apache-2.0 OR MIT" diff --git a/RELEASES.md b/RELEASES.md index 6347800..5192f32 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,5 +1,11 @@ # Releases +## 0.2.4 (2026-04-02) + +- Made some `Slice` methods `const`: + - `map::Slice::{first,last,split_at,split_at_checked,split_first,split_last}` + - `set::Slice::{first,last,split_at,split_at_checked,split_first,split_last}` + ## 0.2.3 (2026-01-08) - Added `RingMap::get_range`, `get_range_mut`, `range`, and `range_mut`. diff --git a/src/lib.rs b/src/lib.rs index 3960350..49c3d46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,10 +170,10 @@ where impl Bucket { // field accessors -- used for `f` instead of closures in `.map(f)` - fn key_ref(&self) -> &K { + const fn key_ref(&self) -> &K { &self.key } - fn value_ref(&self) -> &V { + const fn value_ref(&self) -> &V { &self.value } fn value_mut(&mut self) -> &mut V { @@ -188,7 +188,7 @@ impl Bucket { fn key_value(self) -> (K, V) { (self.key, self.value) } - fn refs(&self) -> (&K, &V) { + const fn refs(&self) -> (&K, &V) { (&self.key, &self.value) } fn ref_mut(&mut self) -> (&K, &mut V) { diff --git a/src/map/slice.rs b/src/map/slice.rs index bed16dd..4ae04b1 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -97,8 +97,12 @@ impl Slice { } /// Get the first key-value pair. - pub fn first(&self) -> Option<(&K, &V)> { - self.entries.first().map(Bucket::refs) + pub const fn first(&self) -> Option<(&K, &V)> { + if let [first, ..] = &self.entries { + Some(first.refs()) + } else { + None + } } /// Get the first key-value pair, with mutable access to the value. @@ -107,8 +111,12 @@ impl Slice { } /// Get the last key-value pair. - pub fn last(&self) -> Option<(&K, &V)> { - self.entries.last().map(Bucket::refs) + pub const fn last(&self) -> Option<(&K, &V)> { + if let [.., last] = &self.entries { + Some(last.refs()) + } else { + None + } } /// Get the last key-value pair, with mutable access to the value. @@ -121,7 +129,7 @@ impl Slice { /// ***Panics*** if `index > len`. /// For a non-panicking alternative see [`split_at_checked`][Self::split_at_checked]. #[track_caller] - pub fn split_at(&self, index: usize) -> (&Self, &Self) { + pub const fn split_at(&self, index: usize) -> (&Self, &Self) { let (first, second) = self.entries.split_at(index); (Self::from_slice(first), Self::from_slice(second)) } @@ -139,9 +147,12 @@ impl Slice { /// Divides one slice into two at an index. /// /// Returns `None` if `index > len`. - pub fn split_at_checked(&self, index: usize) -> Option<(&Self, &Self)> { - let (first, second) = self.entries.split_at_checked(index)?; - Some((Self::from_slice(first), Self::from_slice(second))) + pub const fn split_at_checked(&self, index: usize) -> Option<(&Self, &Self)> { + if let Some((first, second)) = self.entries.split_at_checked(index) { + Some((Self::from_slice(first), Self::from_slice(second))) + } else { + None + } } /// Divides one mutable slice into two at an index. @@ -154,7 +165,7 @@ impl Slice { /// Returns the first key-value pair and the rest of the slice, /// or `None` if it is empty. - pub fn split_first(&self) -> Option<((&K, &V), &Self)> { + pub const fn split_first(&self) -> Option<((&K, &V), &Self)> { if let [first, rest @ ..] = &self.entries { Some((first.refs(), Self::from_slice(rest))) } else { @@ -174,7 +185,7 @@ impl Slice { /// Returns the last key-value pair and the rest of the slice, /// or `None` if it is empty. - pub fn split_last(&self) -> Option<((&K, &V), &Self)> { + pub const fn split_last(&self) -> Option<((&K, &V), &Self)> { if let [rest @ .., last] = &self.entries { Some((last.refs(), Self::from_slice(rest))) } else { diff --git a/src/set/slice.rs b/src/set/slice.rs index a54ae53..167d134 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -70,13 +70,21 @@ impl Slice { } /// Get the first value. - pub fn first(&self) -> Option<&T> { - self.entries.first().map(Bucket::key_ref) + pub const fn first(&self) -> Option<&T> { + if let [first, ..] = &self.entries { + Some(&first.key) + } else { + None + } } /// Get the last value. - pub fn last(&self) -> Option<&T> { - self.entries.last().map(Bucket::key_ref) + pub const fn last(&self) -> Option<&T> { + if let [.., last] = &self.entries { + Some(&last.key) + } else { + None + } } /// Divides one slice into two at an index. @@ -84,7 +92,7 @@ impl Slice { /// ***Panics*** if `index > len`. /// For a non-panicking alternative see [`split_at_checked`][Self::split_at_checked]. #[track_caller] - pub fn split_at(&self, index: usize) -> (&Self, &Self) { + pub const fn split_at(&self, index: usize) -> (&Self, &Self) { let (first, second) = self.entries.split_at(index); (Self::from_slice(first), Self::from_slice(second)) } @@ -92,14 +100,17 @@ impl Slice { /// Divides one slice into two at an index. /// /// Returns `None` if `index > len`. - pub fn split_at_checked(&self, index: usize) -> Option<(&Self, &Self)> { - let (first, second) = self.entries.split_at_checked(index)?; - Some((Self::from_slice(first), Self::from_slice(second))) + pub const fn split_at_checked(&self, index: usize) -> Option<(&Self, &Self)> { + if let Some((first, second)) = self.entries.split_at_checked(index) { + Some((Self::from_slice(first), Self::from_slice(second))) + } else { + None + } } /// Returns the first value and the rest of the slice, /// or `None` if it is empty. - pub fn split_first(&self) -> Option<(&T, &Self)> { + pub const fn split_first(&self) -> Option<(&T, &Self)> { if let [first, rest @ ..] = &self.entries { Some((&first.key, Self::from_slice(rest))) } else { @@ -109,7 +120,7 @@ impl Slice { /// Returns the last value and the rest of the slice, /// or `None` if it is empty. - pub fn split_last(&self) -> Option<(&T, &Self)> { + pub const fn split_last(&self) -> Option<(&T, &Self)> { if let [rest @ .., last] = &self.entries { Some((&last.key, Self::from_slice(rest))) } else {