From 5decb42cf067ffefdce648fd5197352355173305 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Tue, 9 May 2023 21:05:00 +0100 Subject: [PATCH] Implement clone for hashmap --- agb-hashmap/src/lib.rs | 1 + agb-hashmap/src/node.rs | 25 +++++++++++++++++++++++++ agb-hashmap/src/node_storage.rs | 1 + 3 files changed, 27 insertions(+) diff --git a/agb-hashmap/src/lib.rs b/agb-hashmap/src/lib.rs index ac767493..e99f6194 100644 --- a/agb-hashmap/src/lib.rs +++ b/agb-hashmap/src/lib.rs @@ -136,6 +136,7 @@ type HashType = u32; /// println!("{game}: \"{review}\""); /// } /// ``` +#[derive(Clone)] pub struct HashMap { nodes: NodeStorage, diff --git a/agb-hashmap/src/node.rs b/agb-hashmap/src/node.rs index e6b0305e..3331aa94 100644 --- a/agb-hashmap/src/node.rs +++ b/agb-hashmap/src/node.rs @@ -144,3 +144,28 @@ impl Default for Node { Self::new() } } + +impl Clone for Node +where + K: Clone, + V: Clone, +{ + fn clone(&self) -> Self { + if self.has_value() { + Self { + hash: self.hash, + distance_to_initial_bucket: self.distance_to_initial_bucket, + key: MaybeUninit::new(unsafe { self.key.assume_init_ref() }.clone()), + value: MaybeUninit::new(unsafe { self.value.assume_init_ref() }.clone()), + } + } else { + Self { + hash: self.hash, + + distance_to_initial_bucket: self.distance_to_initial_bucket, + key: MaybeUninit::uninit(), + value: MaybeUninit::uninit(), + } + } + } +} diff --git a/agb-hashmap/src/node_storage.rs b/agb-hashmap/src/node_storage.rs index 295848f5..3b073446 100644 --- a/agb-hashmap/src/node_storage.rs +++ b/agb-hashmap/src/node_storage.rs @@ -4,6 +4,7 @@ use alloc::{alloc::Global, vec::Vec}; use crate::{node::Node, number_before_resize, ClonableAllocator, HashType}; +#[derive(Clone)] pub(crate) struct NodeStorage { nodes: Vec, ALLOCATOR>, max_distance_to_initial_bucket: i32,