Skip to content

Commit 0d5ee80

Browse files
authored
Merge pull request #278 from cuviper/slice-new
Add `Slice::new` and `new_mut` for empty slices
2 parents 67e67dd + 0187071 commit 0d5ee80

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/map/slice.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Slice<K, V> {
2727
// and reference lifetimes are bound together in function signatures.
2828
#[allow(unsafe_code)]
2929
impl<K, V> Slice<K, V> {
30-
pub(super) fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
30+
pub(super) const fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
3131
unsafe { &*(entries as *const [Bucket<K, V>] as *const Self) }
3232
}
3333

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

52+
/// Returns an empty slice.
53+
pub const fn new<'a>() -> &'a Self {
54+
Self::from_slice(&[])
55+
}
56+
57+
/// Returns an empty mutable slice.
58+
pub fn new_mut<'a>() -> &'a mut Self {
59+
Self::from_mut_slice(&mut [])
60+
}
61+
5262
/// Return the number of key-value pairs in the map slice.
5363
#[inline]
54-
pub fn len(&self) -> usize {
64+
pub const fn len(&self) -> usize {
5565
self.entries.len()
5666
}
5767

5868
/// Returns true if the map slice contains no elements.
5969
#[inline]
60-
pub fn is_empty(&self) -> bool {
70+
pub const fn is_empty(&self) -> bool {
6171
self.entries.is_empty()
6272
}
6373

src/set/slice.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct Slice<T> {
2424
// and reference lifetimes are bound together in function signatures.
2525
#[allow(unsafe_code)]
2626
impl<T> Slice<T> {
27-
pub(super) fn from_slice(entries: &[Bucket<T>]) -> &Self {
27+
pub(super) const fn from_slice(entries: &[Bucket<T>]) -> &Self {
2828
unsafe { &*(entries as *const [Bucket<T>] as *const Self) }
2929
}
3030

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

45+
/// Returns an empty slice.
46+
pub const fn new<'a>() -> &'a Self {
47+
Self::from_slice(&[])
48+
}
49+
4550
/// Return the number of elements in the set slice.
46-
pub fn len(&self) -> usize {
51+
pub const fn len(&self) -> usize {
4752
self.entries.len()
4853
}
4954

5055
/// Returns true if the set slice contains no elements.
51-
pub fn is_empty(&self) -> bool {
56+
pub const fn is_empty(&self) -> bool {
5257
self.entries.is_empty()
5358
}
5459

0 commit comments

Comments
 (0)