Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
371: epoch: Add Atomic::into_owned r=stjepang a=vorner

To mimic Shared::into_owned.

Closes crossbeam-rs#369

Co-authored-by: Michal 'vorner' Vaner <vorner@vorner.cz>
  • Loading branch information
bors[bot] and vorner committed May 8, 2019
2 parents f746ab4 + aa1b263 commit 6ac269b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crossbeam-epoch/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Add `Atomic::into_owned()`.

# Version 0.7.1

- Add `Shared::deref_mut()`.
Expand Down
38 changes: 38 additions & 0 deletions crossbeam-epoch/src/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,44 @@ impl<T> Atomic<T> {
pub fn fetch_xor<'g>(&self, val: usize, ord: Ordering, _: &'g Guard) -> Shared<'g, T> {
unsafe { Shared::from_usize(self.data.fetch_xor(val & low_bits::<T>(), ord)) }
}

/// Takes ownership of the pointee.
///
/// This consumes the atomic and converts it into [`Owned`]. As [`Atomic`] doesn't have a
/// destructor and doesn't drop the pointee while [`Owned`] does, this is suitable for
/// destructors of data structures.
///
/// # Panics
///
/// Panics if this pointer is null, but only in debug mode.
///
/// # Safety
///
/// This method may be called only if the pointer is valid and nobody else is holding a
/// reference to the same object.
///
/// # Examples
///
/// ```rust
/// # use std::mem;
/// # use crossbeam_epoch::Atomic;
/// struct DataStructure {
/// ptr: Atomic<usize>,
/// }
///
/// impl Drop for DataStructure {
/// fn drop(&mut self) {
/// // By now the DataStructure lives only in our thread and we are sure we don't hold
/// // any Shared or & to it ourselves.
/// unsafe {
/// drop(mem::replace(&mut self.ptr, Atomic::null()).into_owned());
/// }
/// }
/// }
/// ```
pub unsafe fn into_owned(self) -> Owned<T> {
Owned::from_usize(self.data.into_inner())
}
}

impl<T> fmt::Debug for Atomic<T> {
Expand Down

0 comments on commit 6ac269b

Please sign in to comment.