Skip to content

Commit

Permalink
Merge #386
Browse files Browse the repository at this point in the history
386: Conversion of Parker/Unparker into/from a raw pointer r=jeehoonkang a=stjepang

@carllerche Can you review this PR and confirm the API fits your use case?

Co-authored-by: Stjepan Glavina <stjepang@gmail.com>
  • Loading branch information
bors[bot] and Stjepan Glavina committed May 22, 2020
2 parents c5df6cf + e2a7760 commit 5f96f86
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions crossbeam-utils/src/sync/parker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,44 @@ impl Parker {
pub fn unparker(&self) -> &Unparker {
&self.unparker
}

/// Converts a `Parker` into a raw pointer.
///
/// # Examples
///
/// ```
/// use crossbeam_utils::sync::Parker;
///
/// let p = Parker::new();
/// let raw = Parker::into_raw(p);
/// ```
pub fn into_raw(this: Parker) -> *const () {
Unparker::into_raw(this.unparker)
}

/// Converts a raw pointer into a `Parker`.
///
/// # Safety
///
/// This method is safe to use only with pointers returned by [`Parker::into_raw`].
///
/// [`Parker::into_raw`]: struct.Parker.html#method.into_raw
///
/// # Examples
///
/// ```
/// use crossbeam_utils::sync::Parker;
///
/// let p = Parker::new();
/// let raw = Parker::into_raw(p);
/// let p = unsafe { Parker::from_raw(raw) };
/// ```
pub unsafe fn from_raw(ptr: *const ()) -> Parker {
Parker {
unparker: Unparker::from_raw(ptr),
_marker: PhantomData,
}
}
}

impl fmt::Debug for Parker {
Expand Down Expand Up @@ -199,6 +237,46 @@ impl Unparker {
pub fn unpark(&self) {
self.inner.unpark()
}

/// Converts an `Unparker` into a raw pointer.
///
/// # Examples
///
/// ```
/// use crossbeam_utils::sync::{Parker, Unparker};
///
/// let p = Parker::new();
/// let u = p.unparker().clone();
/// let raw = Unparker::into_raw(u);
/// ```
pub fn into_raw(this: Unparker) -> *const () {
Arc::into_raw(this.inner) as *const ()
}

/// Converts a raw pointer into an `Unparker`.
///
/// # Safety
///
/// This method is safe to use only with pointers returned by [`Unparker::into_raw`].
///
/// [`Unparker::into_raw`]: struct.Unparker.html#method.into_raw
///
/// # Examples
///
/// ```
/// use crossbeam_utils::sync::{Parker, Unparker};
///
/// let p = Parker::new();
/// let u = p.unparker().clone();
///
/// let raw = Unparker::into_raw(u);
/// let u = unsafe { Unparker::from_raw(raw) };
/// ```
pub unsafe fn from_raw(ptr: *const ()) -> Unparker {
Unparker {
inner: Arc::from_raw(ptr as *const Inner),
}
}
}

impl fmt::Debug for Unparker {
Expand Down

0 comments on commit 5f96f86

Please sign in to comment.