Skip to content

Commit 6ac269b

Browse files
bors[bot]vorner
andcommitted
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 <[email protected]>
2 parents f746ab4 + aa1b263 commit 6ac269b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

crossbeam-epoch/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Add `Atomic::into_owned()`.
2+
13
# Version 0.7.1
24

35
- Add `Shared::deref_mut()`.

crossbeam-epoch/src/atomic.rs

+38
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,44 @@ impl<T> Atomic<T> {
470470
pub fn fetch_xor<'g>(&self, val: usize, ord: Ordering, _: &'g Guard) -> Shared<'g, T> {
471471
unsafe { Shared::from_usize(self.data.fetch_xor(val & low_bits::<T>(), ord)) }
472472
}
473+
474+
/// Takes ownership of the pointee.
475+
///
476+
/// This consumes the atomic and converts it into [`Owned`]. As [`Atomic`] doesn't have a
477+
/// destructor and doesn't drop the pointee while [`Owned`] does, this is suitable for
478+
/// destructors of data structures.
479+
///
480+
/// # Panics
481+
///
482+
/// Panics if this pointer is null, but only in debug mode.
483+
///
484+
/// # Safety
485+
///
486+
/// This method may be called only if the pointer is valid and nobody else is holding a
487+
/// reference to the same object.
488+
///
489+
/// # Examples
490+
///
491+
/// ```rust
492+
/// # use std::mem;
493+
/// # use crossbeam_epoch::Atomic;
494+
/// struct DataStructure {
495+
/// ptr: Atomic<usize>,
496+
/// }
497+
///
498+
/// impl Drop for DataStructure {
499+
/// fn drop(&mut self) {
500+
/// // By now the DataStructure lives only in our thread and we are sure we don't hold
501+
/// // any Shared or & to it ourselves.
502+
/// unsafe {
503+
/// drop(mem::replace(&mut self.ptr, Atomic::null()).into_owned());
504+
/// }
505+
/// }
506+
/// }
507+
/// ```
508+
pub unsafe fn into_owned(self) -> Owned<T> {
509+
Owned::from_usize(self.data.into_inner())
510+
}
473511
}
474512

475513
impl<T> fmt::Debug for Atomic<T> {

0 commit comments

Comments
 (0)