Implement Clone and Default for bforest::{Set,Map}.

The default container is empty. We need a manual implementation of
Default because deriving it seems to imply that K and V generic
parameter types must also implement Default.

Cloning can be used to clone an empty container or for cloning the whole
forest. We can derive this trait because we already require Copy for K
and V.
This commit is contained in:
Jakob Stoklund Olesen
2017-11-20 14:42:47 -08:00
parent 8eaf7d3904
commit 2d7b54373f
2 changed files with 31 additions and 0 deletions

View File

@@ -61,6 +61,11 @@ where
/// ///
/// This is not a general-purpose replacement for `BTreeMap`. See the [module /// This is not a general-purpose replacement for `BTreeMap`. See the [module
/// documentation](index.html) for more information about design tradeoffs. /// documentation](index.html) for more information about design tradeoffs.
///
/// Maps can be cloned, but that operation should only be used as part of cloning the whole forest
/// they belong to. *Cloning a map does not allocate new memory for the clone*. It creates an alias
/// of the same memory.
#[derive(Clone)]
pub struct Map<K, V, C> pub struct Map<K, V, C>
where where
K: Copy, K: Copy,
@@ -145,6 +150,17 @@ where
} }
} }
impl<K, V, C> Default for Map<K, V, C>
where
K: Copy,
V: Copy,
C: Comparator<K>,
{
fn default() -> Self {
Self::new()
}
}
#[cfg(test)] #[cfg(test)]
impl<K, V, C> Map<K, V, C> impl<K, V, C> Map<K, V, C>
where where

View File

@@ -58,6 +58,11 @@ where
/// ///
/// This is not a general-purpose replacement for `BTreeSet`. See the [module /// This is not a general-purpose replacement for `BTreeSet`. See the [module
/// documentation](index.html) for more information about design tradeoffs. /// documentation](index.html) for more information about design tradeoffs.
///
/// Sets can be cloned, but that operation should only be used as part of cloning the whole forest
/// they belong to. *Cloning a set does not allocate new memory for the clone*. It creates an alias
/// of the same memory.
#[derive(Clone)]
pub struct Set<K, C> pub struct Set<K, C>
where where
K: Copy, K: Copy,
@@ -142,6 +147,16 @@ where
} }
} }
impl<K, C> Default for Set<K, C>
where
K: Copy,
C: Comparator<K>,
{
fn default() -> Self {
Self::new()
}
}
/// A position in a `Set` used to navigate and modify the ordered set. /// A position in a `Set` used to navigate and modify the ordered set.
/// ///
/// A cursor always points at an element in the set, or "off the end" which is a position after the /// A cursor always points at an element in the set, or "off the end" which is a position after the