From 060c02ac8a3482c9b38216b57ebce3b8b4b44ac2 Mon Sep 17 00:00:00 2001 From: Francesco Date: Tue, 28 May 2024 14:56:25 +0200 Subject: [PATCH 1/2] Fix clear --- .../src/structure/stable_storage/btreemap.rs | 7 +------ .../src/structure/stable_storage/log.rs | 10 ++++------ .../src/structure/stable_storage/multimap.rs | 5 +---- .../src/structure/stable_storage/unbounded.rs | 5 +---- .../src/structure/stable_storage/vec.rs | 10 ++++------ 5 files changed, 11 insertions(+), 26 deletions(-) diff --git a/ic-stable-structures/src/structure/stable_storage/btreemap.rs b/ic-stable-structures/src/structure/stable_storage/btreemap.rs index 0e56f885..3fa49530 100644 --- a/ic-stable-structures/src/structure/stable_storage/btreemap.rs +++ b/ic-stable-structures/src/structure/stable_storage/btreemap.rs @@ -55,12 +55,7 @@ where } fn clear(&mut self) { - let inner = &mut self.0; - - let keys: Vec<_> = inner.iter().map(|(k, _)| k).collect(); - for key in keys { - inner.remove(&key); - } + self.0.clear_new(); } fn contains_key(&self, key: &K) -> bool { diff --git a/ic-stable-structures/src/structure/stable_storage/log.rs b/ic-stable-structures/src/structure/stable_storage/log.rs index 3b8d1b35..4517ecb6 100644 --- a/ic-stable-structures/src/structure/stable_storage/log.rs +++ b/ic-stable-structures/src/structure/stable_storage/log.rs @@ -43,11 +43,9 @@ impl LogStructure for StableLog { } fn clear(&mut self) { - let (index_mem, data_mem) = self - .0 - .take() - .expect("inner log is always present") - .into_memories(); - self.0 = Some(log::Log::new(index_mem, data_mem)); + if let Some(log) = self.0.take() { + let (index_mem, data_mem) = log.into_memories(); + self.0 = Some(log::Log::new(index_mem, data_mem)); + } } } diff --git a/ic-stable-structures/src/structure/stable_storage/multimap.rs b/ic-stable-structures/src/structure/stable_storage/multimap.rs index 18d1ae90..eaff1503 100644 --- a/ic-stable-structures/src/structure/stable_storage/multimap.rs +++ b/ic-stable-structures/src/structure/stable_storage/multimap.rs @@ -115,10 +115,7 @@ where } fn clear(&mut self) { - let keys: Vec<_> = self.0.iter().map(|(k, _)| k).collect(); - for key in keys { - self.0.remove(&key); - } + self.0.clear_new(); } fn range(&self, first_key: &K1) -> Self::RangeIterator<'_> { diff --git a/ic-stable-structures/src/structure/stable_storage/unbounded.rs b/ic-stable-structures/src/structure/stable_storage/unbounded.rs index eaaf62af..d1bd3d23 100644 --- a/ic-stable-structures/src/structure/stable_storage/unbounded.rs +++ b/ic-stable-structures/src/structure/stable_storage/unbounded.rs @@ -176,10 +176,7 @@ where } fn clear(&mut self) { - let keys: Vec<_> = self.inner.iter().map(|(k, _)| k).collect(); - for key in keys { - self.inner.remove(&key); - } + self.inner.clear_new(); self.items_count = 0; } } diff --git a/ic-stable-structures/src/structure/stable_storage/vec.rs b/ic-stable-structures/src/structure/stable_storage/vec.rs index f276f809..3c61d98a 100644 --- a/ic-stable-structures/src/structure/stable_storage/vec.rs +++ b/ic-stable-structures/src/structure/stable_storage/vec.rs @@ -33,12 +33,10 @@ impl VecStructure for StableVec { } fn clear(&mut self) -> Result<()> { - let memory = self - .0 - .take() - .expect("vector is always initialized") - .into_memory(); - self.0 = Some(vec::Vec::new(memory)?); + if let Some(vector) = self.0.take() { + let memory = vector.into_memory(); + self.0 = Some(vec::Vec::new(memory)?); + } Ok(()) } From c31cf549e82e0031581a4f05c1418cdd63f25534 Mon Sep 17 00:00:00 2001 From: Francesco Date: Tue, 28 May 2024 15:02:50 +0200 Subject: [PATCH 2/2] fmt --- ic-stable-structures/src/structure/stable_storage/log.rs | 2 +- ic-stable-structures/src/structure/stable_storage/vec.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ic-stable-structures/src/structure/stable_storage/log.rs b/ic-stable-structures/src/structure/stable_storage/log.rs index 4517ecb6..f349ca29 100644 --- a/ic-stable-structures/src/structure/stable_storage/log.rs +++ b/ic-stable-structures/src/structure/stable_storage/log.rs @@ -45,7 +45,7 @@ impl LogStructure for StableLog { fn clear(&mut self) { if let Some(log) = self.0.take() { let (index_mem, data_mem) = log.into_memories(); - self.0 = Some(log::Log::new(index_mem, data_mem)); + self.0 = Some(log::Log::new(index_mem, data_mem)); } } } diff --git a/ic-stable-structures/src/structure/stable_storage/vec.rs b/ic-stable-structures/src/structure/stable_storage/vec.rs index 3c61d98a..d6af11ed 100644 --- a/ic-stable-structures/src/structure/stable_storage/vec.rs +++ b/ic-stable-structures/src/structure/stable_storage/vec.rs @@ -36,7 +36,7 @@ impl VecStructure for StableVec { if let Some(vector) = self.0.take() { let memory = vector.into_memory(); self.0 = Some(vec::Vec::new(memory)?); - } + } Ok(()) }