impl Clone for all containers

This commit is contained in:
Artem Vorotnikov 2019-01-21 00:04:28 +03:00
parent a1e5408d23
commit 4b09a24573
No known key found for this signature in database
GPG Key ID: E0148C3F2FBB7A20
7 changed files with 167 additions and 0 deletions

View File

@ -412,6 +412,20 @@ impl<'a, T> Drop for Hole<'a, T> {
}
}
impl<T, N, K> Clone for BinaryHeap<T, N, K>
where
N: ArrayLength<T>,
K: Kind,
T: Ord + Clone,
{
fn clone(&self) -> Self {
Self {
_kind: self._kind,
data: self.data.clone(),
}
}
}
impl<T, N, K> fmt::Debug for BinaryHeap<T, N, K>
where
N: ArrayLength<T>,

View File

@ -28,6 +28,7 @@ impl HashValue {
}
#[doc(hidden)]
#[derive(Clone)]
pub struct Bucket<K, V> {
hash: HashValue,
key: K,
@ -268,6 +269,20 @@ where
}
}
impl<K, V, N> Clone for CoreMap<K, V, N>
where
K: Eq + Hash + Clone,
V: Clone,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
{
fn clone(&self) -> Self {
Self {
entries: self.entries.clone(),
indices: self.indices.clone(),
}
}
}
/// Fixed capacity [`IndexMap`](https://docs.rs/indexmap/1/indexmap/map/struct.IndexMap.html)
///
/// Note that the capacity of the `IndexMap` must be a power of 2.
@ -725,6 +740,21 @@ where
}
}
impl<K, V, N, S> Clone for IndexMap<K, V, N, S>
where
K: Eq + Hash + Clone,
V: Clone,
S: Clone,
N: ArrayLength<Bucket<K, V>> + ArrayLength<Option<Pos>>,
{
fn clone(&self) -> Self {
Self {
core: self.core.clone(),
build_hasher: self.build_hasher.clone(),
}
}
}
impl<K, V, N, S> fmt::Debug for IndexMap<K, V, N, S>
where
K: Eq + Hash + fmt::Debug,
@ -846,6 +876,18 @@ where
}
}
impl<'a, K, V> Clone for Iter<'a, K, V>
where
K: 'a,
V: 'a,
{
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}
pub struct IterMut<'a, K, V>
where
K: 'a,

View File

@ -454,6 +454,19 @@ where
}
}
impl<T, N, S> Clone for IndexSet<T, N, S>
where
T: Eq + Hash + Clone,
S: Clone,
N: ArrayLength<Bucket<T, ()>> + ArrayLength<Option<Pos>>,
{
fn clone(&self) -> Self {
Self {
map: self.map.clone(),
}
}
}
impl<T, N, S> fmt::Debug for IndexSet<T, N, S>
where
T: Eq + Hash + fmt::Debug,
@ -567,6 +580,17 @@ where
}
}
impl<'a, T> Clone for Iter<'a, T>
where
T: 'a,
{
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}
pub struct Difference<'a, T, N, S>
where
S: 'a + BuildHasher,

View File

@ -390,6 +390,19 @@ where
}
}
impl<K, V, N> Clone for LinearMap<K, V, N>
where
N: ArrayLength<(K, V)>,
K: Eq + Clone,
V: Clone,
{
fn clone(&self) -> Self {
Self {
buffer: self.buffer.clone(),
}
}
}
impl<K, V, N> fmt::Debug for LinearMap<K, V, N>
where
N: ArrayLength<(K, V)>,
@ -434,6 +447,18 @@ where
}
}
impl<'a, K, V> Clone for Iter<'a, K, V>
where
K: 'a,
V: 'a,
{
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}
pub struct IterMut<'a, K, V>
where
K: 'a,

View File

@ -408,6 +408,22 @@ where
len: usize,
}
impl<'a, T, N, U, C> Clone for Iter<'a, T, N, U, C>
where
N: ArrayLength<T> + 'a,
T: 'a,
U: 'a + sealed::Uxx,
C: 'a + sealed::XCore,
{
fn clone(&self) -> Self {
Self {
rb: self.rb,
index: self.index,
len: self.len,
}
}
}
/// A mutable iterator over the items of a queue
pub struct IterMut<'a, T, N, U, C>
where

View File

@ -414,6 +414,17 @@ where
}
}
impl<N> Clone for String<N>
where
N: ArrayLength<u8>,
{
fn clone(&self) -> Self {
Self {
vec: self.vec.clone(),
}
}
}
impl<N> fmt::Debug for String<N>
where
N: ArrayLength<u8>,
@ -549,6 +560,16 @@ mod tests {
static mut _S: String<U8> = String::new();
}
#[test]
fn clone() {
let s1: String<U20> = String::from("abcd");
let mut s2 = s1.clone();
s2.push_str(" efgh").unwrap();
assert_eq!(s1, "abcd");
assert_eq!(s2, "abcd efgh");
}
#[test]
fn debug() {
extern crate std;

View File

@ -43,6 +43,18 @@ where
len: usize,
}
impl<T, N> Clone for Vec<T, N>
where
N: ArrayLength<T>,
T: Clone,
{
fn clone(&self) -> Self {
let mut new = Self::new();
new.extend_from_slice(self.as_ref()).unwrap();
new
}
}
impl<T, N> Vec<T, N>
where
N: ArrayLength<T>,
@ -361,6 +373,19 @@ where
}
}
impl<T, N> Clone for IntoIter<T, N>
where
T: Clone,
N: ArrayLength<T>,
{
fn clone(&self) -> Self {
Self {
vec: self.vec.clone(),
next: self.next,
}
}
}
impl<T, N> Drop for IntoIter<T, N>
where
N: ArrayLength<T>,