diff --git a/crates/block2/CHANGELOG.md b/crates/block2/CHANGELOG.md index 4bd7f9dfe..0ed19bea9 100644 --- a/crates/block2/CHANGELOG.md +++ b/crates/block2/CHANGELOG.md @@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). blocks with an encoding specified by `ManualBlockEncoding`. This is useful for certain APIs that require blocks to have an encoding. +* Added `RcBlock::as_ptr`. +* Added `RcBlock::into_raw`. ### Fixed * **BREAKING**: Converted function signatures into using `extern "C-unwind"`. diff --git a/crates/block2/src/rc_block.rs b/crates/block2/src/rc_block.rs index 3767c206b..143901796 100644 --- a/crates/block2/src/rc_block.rs +++ b/crates/block2/src/rc_block.rs @@ -37,6 +37,46 @@ pub struct RcBlock { } impl RcBlock { + /// A raw pointer to the underlying block. + /// + /// The pointer is valid for at least as long as the `RcBlock` is alive. + /// + /// This is an associated method, and must be called as + /// `RcBlock::as_ptr(&block)`. + #[inline] + pub fn as_ptr(this: &Self) -> *mut Block { + this.ptr.as_ptr() + } + + /// Consumes the `RcBlock`, passing ownership of the retain count to the + /// caller. + /// + /// After calling this function, the caller is responsible for releasing + /// the memory with [`ffi::_Block_release`] or similar. + /// + /// This is an associated method, and must be called as + /// `RcBlock::into_raw(block)`. + /// + /// + /// # Examples + /// + /// Converting a `RcBlock` to a pointer and back. + /// + /// ``` + /// use block2::RcBlock; + /// + /// let add2 = RcBlock::new(|x: i32| -> i32 { + /// x + 2 + /// }); + /// let ptr = RcBlock::into_raw(add2); + /// // SAFETY: The pointer is valid, and ownership from above. + /// let add2 = unsafe { RcBlock::from_raw(ptr) }.unwrap(); + /// ``` + pub fn into_raw(this: Self) -> *mut Block { + let this = ManuallyDrop::new(this); + this.ptr.as_ptr() + } + /// Construct an `RcBlock` from the given block pointer by taking /// ownership. ///