From 9a4bf51a5f918dd243192848b3477944a31ad04d Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Wed, 22 Dec 2021 20:48:54 +0100 Subject: [PATCH] better naming --- src/lib.rs | 25 ++++++++++++++++++++----- src/raw.rs | 4 +++- src/test.rs | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 133b236..0f76e61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,11 +95,26 @@ impl Vechonk { self.raw.pop() } - /// Insert an element at an index. - /// * If the insertion was successful, the old element is returned. + pub fn insert(&mut self, _index: usize, _element: Box) { + todo!() + } + + /// Replace an element at an index. + /// This could be O(n) worst case, if the new element is too big, and every other element needs to be realigned. + /// Even worse, after all the copying, it might realloc anyways because it couldn't fit in the space. + /// + /// Returns the old element at that + pub fn replace(&mut self, _index: usize, _element: Box) -> Box { + todo!() + } + + /// Replace an element at an index. + /// * If the replacement was successful without moving the other elements, the old element is returned. /// * If the new element doesn't fit the gap or can't be aligned, it is returned. - pub fn try_insert(&mut self, index: usize, element: Box) -> Result, Box> { - self.raw.try_insert_elem(element, index) + /// + /// This is guaranteed to be O(1) + pub fn try_replace(&mut self, index: usize, element: Box) -> Result, Box> { + self.raw.try_replace_elem(element, index) } /// An iterator over the elements yielding shared references @@ -179,7 +194,7 @@ impl MutGuard { /// * If the element fits in the space, the old element is returned /// * If the element does not fit in the space, the new element is returned again pub fn try_write(&mut self, element: Box) -> Result, Box> { - self.raw.try_insert_elem(element, self.index) + self.raw.try_replace_elem(element, self.index) } } diff --git a/src/raw.rs b/src/raw.rs index 2c57426..d755d14 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -127,7 +127,7 @@ impl RawVechonk { /// Insert an element at an index. /// * If the insertion was successful, the old element is returned. /// * If the new element doesn't fit the gap or can't be aligned, it is returned. - pub fn try_insert_elem(&mut self, element: Box, index: usize) -> Result, Box> { + pub fn try_replace_elem(&mut self, element: Box, index: usize) -> Result, Box> { if index >= self.len { // out of bounds return Err(element); @@ -328,6 +328,8 @@ impl RawVechonk { /// The caller must either set the `len` to zero, or copy the elements to the new allocation by saving /// `self.ptr` before calling this function. unsafe fn realloc(&mut self, size: NonZeroUsize) { + // TODO this is *not* sound, since the alignment of some big elements might be wrong now + let layout = Layout::from_size_align(size.get(), Self::data_align()).unwrap(); // SAFETY: layout is guaranteed to have a non-zero size diff --git a/src/test.rs b/src/test.rs index 5b3cc75..413b814 100644 --- a/src/test.rs +++ b/src/test.rs @@ -262,7 +262,7 @@ fn get_mut_mutating() { fn insert() { let mut chonk: Vechonk = vechonk!["hello".into(), "uwu".into()]; - chonk.try_insert(0, "owo".into()).unwrap(); + chonk.try_replace(0, "owo".into()).unwrap(); assert_eq!(&chonk[0], "owo"); }