Skip to content

Commit

Permalink
add sliced Deref, DerefMut, AsRef, AsMut
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Mar 1, 2021
1 parent ea9437c commit ca196d5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
38 changes: 35 additions & 3 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{Bucket, Entries, IndexMap, Iter, IterMut, Keys, Values, ValuesMut};
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{self, Index, IndexMut};
use core::ops::{self, Deref, DerefMut, Index, IndexMut};

/// A dynamically-sized slice of key-value pairs in an `IndexMap`.
///
Expand Down Expand Up @@ -44,13 +44,45 @@ impl<K, V, S> IndexMap<K, V, S> {
}
}

impl<K, V> Iter<'_, K, V> {
impl<K, V, S> Deref for IndexMap<K, V, S> {
type Target = Slice<K, V>;

fn deref(&self) -> &Self::Target {
self.as_slice()
}
}

impl<K, V, S> DerefMut for IndexMap<K, V, S> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}

impl<K, V, S> AsRef<Slice<K, V>> for IndexMap<K, V, S> {
fn as_ref(&self) -> &Slice<K, V> {
self.as_slice()
}
}

impl<K, V, S> AsMut<Slice<K, V>> for IndexMap<K, V, S> {
fn as_mut(&mut self) -> &mut Slice<K, V> {
self.as_mut_slice()
}
}

impl<'a, K, V> Iter<'a, K, V> {
/// Returns a slice of the remaining entries in the iterator.
pub fn as_slice(&self) -> &Slice<K, V> {
pub fn as_slice(&self) -> &'a Slice<K, V> {
Slice::from_slice(self.iter.as_slice())
}
}

impl<K, V> AsRef<Slice<K, V>> for Iter<'_, K, V> {
fn as_ref(&self) -> &Slice<K, V> {
self.as_slice()
}
}

impl<'a, K, V> IterMut<'a, K, V> {
/// Returns a slice of the remaining entries in the iterator.
///
Expand Down
26 changes: 23 additions & 3 deletions src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{Bucket, Entries, IndexSet, Iter};
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{self, Index};
use core::ops::{self, Deref, Index};

/// A dynamically-sized slice of values in an `IndexSet`.
///
Expand Down Expand Up @@ -33,13 +33,33 @@ impl<T, S> IndexSet<T, S> {
}
}

impl<T> Iter<'_, T> {
impl<T, S> Deref for IndexSet<T, S> {
type Target = Slice<T>;

fn deref(&self) -> &Self::Target {
self.as_slice()
}
}

impl<T, S> AsRef<Slice<T>> for IndexSet<T, S> {
fn as_ref(&self) -> &Slice<T> {
self.as_slice()
}
}

impl<'a, T> Iter<'a, T> {
/// Returns a slice of the remaining entries in the iterator.
pub fn as_slice(&self) -> &Slice<T> {
pub fn as_slice(&self) -> &'a Slice<T> {
Slice::from_slice(self.iter.as_slice())
}
}

impl<T> AsRef<Slice<T>> for Iter<'_, T> {
fn as_ref(&self) -> &Slice<T> {
self.as_slice()
}
}

impl<T> Slice<T> {
/// Return the number of elements in the set slice.
pub fn len(&self) -> usize {
Expand Down

0 comments on commit ca196d5

Please sign in to comment.