Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Slice::new and new_mut for empty slices #278

Merged
merged 1 commit into from
Oct 27, 2023
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
16 changes: 13 additions & 3 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Slice<K, V> {
// and reference lifetimes are bound together in function signatures.
#[allow(unsafe_code)]
impl<K, V> Slice<K, V> {
pub(super) fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
pub(super) const fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
unsafe { &*(entries as *const [Bucket<K, V>] as *const Self) }
}

Expand All @@ -49,15 +49,25 @@ impl<K, V> Slice<K, V> {
self.into_boxed().into_vec()
}

/// Returns an empty slice.
pub const fn new<'a>() -> &'a Self {
Self::from_slice(&[])
}

/// Returns an empty mutable slice.
pub fn new_mut<'a>() -> &'a mut Self {
Self::from_mut_slice(&mut [])
}

/// Return the number of key-value pairs in the map slice.
#[inline]
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.entries.len()
}

/// Returns true if the map slice contains no elements.
#[inline]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.entries.is_empty()
}

Expand Down
11 changes: 8 additions & 3 deletions src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Slice<T> {
// and reference lifetimes are bound together in function signatures.
#[allow(unsafe_code)]
impl<T> Slice<T> {
pub(super) fn from_slice(entries: &[Bucket<T>]) -> &Self {
pub(super) const fn from_slice(entries: &[Bucket<T>]) -> &Self {
unsafe { &*(entries as *const [Bucket<T>] as *const Self) }
}

Expand All @@ -42,13 +42,18 @@ impl<T> Slice<T> {
self.into_boxed().into_vec()
}

/// Returns an empty slice.
pub const fn new<'a>() -> &'a Self {
Self::from_slice(&[])
}

/// Return the number of elements in the set slice.
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.entries.len()
}

/// Returns true if the set slice contains no elements.
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.entries.is_empty()
}

Expand Down