Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ where

impl<K, V> Bucket<K, V> {
// 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 {
Expand All @@ -188,7 +188,7 @@ impl<K, V> Bucket<K, V> {
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) {
Expand Down
31 changes: 21 additions & 10 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ impl<K, V> Slice<K, V> {
}

/// 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.
Expand All @@ -107,8 +111,12 @@ impl<K, V> Slice<K, V> {
}

/// 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.
Expand All @@ -121,7 +129,7 @@ impl<K, V> Slice<K, V> {
/// ***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))
}
Expand All @@ -139,9 +147,12 @@ impl<K, V> Slice<K, V> {
/// 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.
Expand All @@ -154,7 +165,7 @@ impl<K, V> Slice<K, V> {

/// 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 {
Expand All @@ -174,7 +185,7 @@ impl<K, V> Slice<K, V> {

/// 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 {
Expand Down
31 changes: 21 additions & 10 deletions src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,47 @@ impl<T> Slice<T> {
}

/// 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.
///
/// ***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))
}

/// 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 {
Expand All @@ -109,7 +120,7 @@ impl<T> Slice<T> {

/// 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 {
Expand Down
Loading