Skip to content

Commit

Permalink
Adding into_raw() and from_raw() methods
Browse files Browse the repository at this point in the history
See issue #28.
  • Loading branch information
bendk committed Aug 29, 2023
1 parent 80f4c3f commit 0af70cd
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ impl<T> Sender<T> {
_ => unreachable!(),
}
}

/// Consumes the Sender, returning a raw pointer.
///
/// This call will leak memory unless the pointer is passed back to [Sender::from_raw].
pub fn into_raw(self) -> *mut () {
self.channel_ptr.as_ptr() as *mut ()
}

/// Consumes a raw pointer from [Sender::into_raw], recreating the Sender
///
/// SAFETY: This pointer must have come from [Sender::into_raw] and must not have been passed
/// to `from_raw` before.
pub unsafe fn from_raw(raw: *mut ()) -> Self {
Self {
channel_ptr: NonNull::new_unchecked(raw as *mut Channel<T>),
_invariant: PhantomData,
}
}
}

impl<T> Drop for Sender<T> {
Expand Down Expand Up @@ -816,6 +834,23 @@ impl<T> Receiver<T> {
_ => unreachable!(),
}
}

/// Consumes the Receiver, returning a raw pointer.
///
/// This call will leak memory unless the pointer is passed back to [Receiver::from_raw].
pub fn into_raw(self) -> *mut () {
self.channel_ptr.as_ptr() as *mut ()
}

/// Consumes a raw pointer from [Receiver::into_raw], recreating the Receiver
///
/// SAFETY: This pointer must have come from [Receiver::into_raw] and must not have been passed
/// to `from_raw` before.
pub unsafe fn from_raw(raw: *mut ()) -> Self {
Self {
channel_ptr: NonNull::new_unchecked(raw as *mut Channel<T>),
}
}
}

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

0 comments on commit 0af70cd

Please sign in to comment.