From 2d7b54373f0581a894a7c7c6eed641c44d678594 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Mon, 20 Nov 2017 14:42:47 -0800 Subject: [PATCH] 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. --- lib/cretonne/src/bforest/map.rs | 16 ++++++++++++++++ lib/cretonne/src/bforest/set.rs | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/cretonne/src/bforest/map.rs b/lib/cretonne/src/bforest/map.rs index 23cac5600d..601e9febe6 100644 --- a/lib/cretonne/src/bforest/map.rs +++ b/lib/cretonne/src/bforest/map.rs @@ -61,6 +61,11 @@ where /// /// This is not a general-purpose replacement for `BTreeMap`. See the [module /// 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 where K: Copy, @@ -145,6 +150,17 @@ where } } +impl Default for Map +where + K: Copy, + V: Copy, + C: Comparator, +{ + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] impl Map where diff --git a/lib/cretonne/src/bforest/set.rs b/lib/cretonne/src/bforest/set.rs index 4956a26147..025efc0225 100644 --- a/lib/cretonne/src/bforest/set.rs +++ b/lib/cretonne/src/bforest/set.rs @@ -58,6 +58,11 @@ where /// /// This is not a general-purpose replacement for `BTreeSet`. See the [module /// 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 where K: Copy, @@ -142,6 +147,16 @@ where } } +impl Default for Set +where + K: Copy, + C: Comparator, +{ + fn default() -> Self { + Self::new() + } +} + /// 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