Skip to content

Commit

Permalink
Fix change ContiguousMemoryStorage::forget return type to include `…
Browse files Browse the repository at this point in the history
…Layout`.

Returned base address is now uniformly of type *const ()

Signed-off-by: Tin Svagelj <[email protected]>
  • Loading branch information
Caellian committed Sep 17, 2023
1 parent 2169160 commit ed1fceb
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,27 +247,6 @@ impl<Impl: ImplDetails> ContiguousMemoryStorage<Impl> {
) -> Impl::LockResult<Impl::ReferenceType<T>> {
Impl::assume_stored(&self.inner, position)
}

/// Forgets this container without dropping it and returns the base address.
///
/// Calling this method will create a memory leak because the smart pointer
/// to state will not be dropped even when all of the created references go
/// out of scope. As this method takes ownership of the container, calling
/// it also ensures that dereferencing pointers created by
/// [`as_ptr`](refs::ContiguousEntryRef::as_ptr),
/// [`as_ptr_mut`](refs::ContiguousEntryRef::as_ptr_mut),
/// [`into_ptr`](refs::ContiguousEntryRef::into_ptr), and
/// [`into_ptr_mut`](refs::ContiguousEntryRef::into_ptr_mut)
/// `ContiguousEntryRef` methods is guaranteed to be safe.
///
/// This method isn't unsafe as leaking data doesn't cause undefined
/// behavior.
/// ([_see details_](https://doc.rust-lang.org/nomicon/leaking.html))
pub fn forget(self) -> Impl::LockResult<*mut u8> {
let base = Impl::get_base(&self.base);
core::mem::forget(self);
base
}
}

impl ContiguousMemoryStorage<ImplDefault> {
Expand Down Expand Up @@ -306,6 +285,31 @@ impl ContiguousMemoryStorage<ImplDefault> {
self.capacity.get()
}
}

/// Forgets this container without dropping it and returns its base address
/// and [`Layout`].
///
/// # Safety
///
/// Calling this method will create a memory leak because the smart pointer
/// to state will not be dropped even when all of the created references go
/// out of scope. As this method takes ownership of the container, calling
/// it also ensures that dereferencing pointers created by
/// [`as_ptr`](refs::ContiguousEntryRef::as_ptr),
/// [`as_ptr_mut`](refs::ContiguousEntryRef::as_ptr_mut),
/// [`into_ptr`](refs::ContiguousEntryRef::into_ptr), and
/// [`into_ptr_mut`](refs::ContiguousEntryRef::into_ptr_mut)
/// `ContiguousEntryRef` methods is guaranteed to be safe.
///
/// This method isn't unsafe as leaking data doesn't cause undefined
/// behavior.
/// ([_see details_](https://doc.rust-lang.org/nomicon/leaking.html))
pub fn forget(self) -> (*const (), Layout) {
let base = ImplDefault::get_base(&self.base);
let layout = self.get_layout();
core::mem::forget(self);
(base as *const (), layout)
}
}

impl ContiguousMemoryStorage<ImplConcurrent> {
Expand Down Expand Up @@ -364,6 +368,19 @@ impl ContiguousMemoryStorage<ImplConcurrent> {
Ok(self.get_capacity())
}
}

/// Forgets this container without dropping it and returns its base address
/// and [`Layout`], or a [`LockingError::Poisoned`] error if base address
/// `RwLock` has been poisoned.
///
/// For details on safety see _Safety_ section of
/// [default implementation](ContiguousMemoryStorage<ImplConcurrent>::forget).
pub fn forget(self) -> Result<(*const (), Layout), LockingError> {
let base = ImplConcurrent::get_base(&self.base);
let layout = self.get_layout();
core::mem::forget(self);
base.map(|it| (it as *const (), layout))
}
}

impl ContiguousMemoryStorage<ImplUnsafe> {
Expand Down Expand Up @@ -470,6 +487,18 @@ impl ContiguousMemoryStorage<ImplUnsafe> {
core::ptr::drop_in_place(freed as *mut T);
}
}

/// Forgets this container without dropping it and returns its base address
/// and [`Layout`].
///
/// For details on safety see _Safety_ section of
/// [default implementation](ContiguousMemoryStorage<ImplConcurrent>::forget).
pub fn forget(self) -> (*const (), Layout) {
let base = ImplUnsafe::get_base(&self.base);
let layout = self.get_layout();
core::mem::forget(self);
(base as *const (), layout)
}
}

#[cfg(feature = "debug")]
Expand Down

0 comments on commit ed1fceb

Please sign in to comment.