Skip to content

Commit

Permalink
Remove unnecessary Clone bounds (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felerius authored Sep 16, 2024
1 parent 5f3af81 commit 108f8b6
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions src/vec_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,47 @@ pub struct VecMap<K, V> {
_marker: PhantomData<K>,
}

impl<K: IndexKey, V: Clone> VecMap<K, V> {
impl<K: IndexKey, V> VecMap<K, V> {
/// Initializes an empty [`VecMap`].
///
/// For performance reasons it's almost always better to avoid dynamic
/// resizing by using [`Self::with_capacity()`] instead.
#[must_use]
pub const fn new() -> Self {
Self {
data: vec![],
len: 0,
_marker: PhantomData,
}
}

/// Returns the number of elements the map can hold without reallocating.
///
/// The index range of items that the map can hold without reallocating is
/// `0..capacity`.
#[must_use]
pub fn capacity(&self) -> usize {
self.data.len()
}

/// Initializes [`VecMap`] with capacity to hold exactly `n` elements in the
/// index range of `0..n`.
#[must_use]
pub fn with_capacity(n: usize) -> Self {
let mut data = Vec::with_capacity(n);
data.resize_with(n, || None);
Self {
data: vec![None; n],
data,
len: 0,
_marker: PhantomData,
}
}

/// Initializes [`VecMap`] with `n` occurences of `elem`.
pub fn from_elem(elem: V, n: usize) -> Self {
pub fn from_elem(elem: V, n: usize) -> Self
where
V: Clone,
{
Self {
data: vec![Some(elem); n],
len: n,
Expand All @@ -94,36 +122,14 @@ impl<K: IndexKey, V: Clone> VecMap<K, V> {
/// Clears all data from the [`VecMap`] without changing the capacity.
pub fn clear(&mut self) {
self.len = 0;
self.data = vec![None; self.capacity()];
let capacity = self.data.len();
self.data.clear();
self.data.resize_with(capacity, || None);
}

/// Reserve capacity for `additional` key-value pairs.
pub fn reserve(&mut self, additional: usize) {
self.data.extend(vec![None; additional]);
}
}

impl<K: IndexKey, V> VecMap<K, V> {
/// Initializes an empty [`VecMap`].
///
/// For performance reasons it's almost always better to avoid dynamic
/// resizing by using [`Self::with_capacity()`] instead.
#[must_use]
pub const fn new() -> Self {
Self {
data: vec![],
len: 0,
_marker: PhantomData,
}
}

/// Returns the number of elements the map can hold without reallocating.
///
/// The index range of items that the map can hold without reallocating is
/// `0..capacity`.
#[must_use]
pub fn capacity(&self) -> usize {
self.data.len()
self.data.resize_with(self.data.len() + additional, || None);
}

/// Inserts a key-value pair into the map.
Expand Down Expand Up @@ -434,7 +440,7 @@ impl<K: IndexKey + fmt::Debug, V: fmt::Debug> fmt::Debug for Entry<'_, K, V> {
}
}

impl<K: IndexKey, V: Clone> FromIterator<(K, V)> for VecMap<K, V> {
impl<K: IndexKey, V> FromIterator<(K, V)> for VecMap<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let iter = iter.into_iter();
let (lower_bound, _) = iter.size_hint();
Expand All @@ -447,7 +453,7 @@ impl<K: IndexKey, V: Clone> FromIterator<(K, V)> for VecMap<K, V> {
}
}

impl<K: IndexKey, V: Clone> Extend<(K, V)> for VecMap<K, V> {
impl<K: IndexKey, V> Extend<(K, V)> for VecMap<K, V> {
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
// extend does not attempt to reserve additional space because the space needed
// is dependent on the keys that are added
Expand Down

0 comments on commit 108f8b6

Please sign in to comment.