diff --git a/Cargo.toml b/Cargo.toml index b6b2325..643f4f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ringmap" edition = "2021" -version = "0.2.1" +version = "0.2.2" 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 fd61086..ac8d6af 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,5 +1,10 @@ # Releases +## 0.2.2 (2026-01-07) + +- Added `map::Slice::split_at_checked` and `split_at_mut_checked`. +- Added `set::Slice::split_at_checked`. + ## 0.2.1 (2025-11-20) - Simplified a lot of internals using `hashbrown`'s new bucket API. diff --git a/src/map/slice.rs b/src/map/slice.rs index cfbcbe8..bbafe0c 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -119,6 +119,7 @@ impl Slice { /// Divides one slice into two at an index. /// /// ***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) { let (first, second) = self.entries.split_at(index); @@ -128,12 +129,29 @@ impl Slice { /// Divides one mutable slice into two at an index. /// /// ***Panics*** if `index > len`. + /// For a non-panicking alternative see [`split_at_mut_checked`][Self::split_at_mut_checked]. #[track_caller] pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) { let (first, second) = self.entries.split_at_mut(index); (Self::from_mut_slice(first), Self::from_mut_slice(second)) } + /// 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))) + } + + /// Divides one mutable slice into two at an index. + /// + /// Returns `None` if `index > len`. + pub fn split_at_mut_checked(&mut self, index: usize) -> Option<(&mut Self, &mut Self)> { + let (first, second) = self.entries.split_at_mut_checked(index)?; + Some((Self::from_mut_slice(first), Self::from_mut_slice(second))) + } + /// 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)> { diff --git a/src/set/slice.rs b/src/set/slice.rs index 26cd94b..96a8f60 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -82,12 +82,21 @@ impl Slice { /// Divides one slice into two at an index. /// /// ***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) { let (first, second) = self.entries.split_at(index); (Self::from_slice(first), Self::from_slice(second)) } + /// 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))) + } + /// Returns the first value and the rest of the slice, /// or `None` if it is empty. pub fn split_first(&self) -> Option<(&T, &Self)> {