Skip to content

Commit b1b0d26

Browse files
quark-zjufacebook-github-bot
authored andcommitted
iobuf: add unsafe IOBufShared::from_owner API
Summary: Currently, `iobuf` depends on `minibytes` just to do: unsafe impl BytesToIOufSharedExt for minibytes::Bytes It makes the OSS world's `Cargo.toml` story tricky. I don't think `minibytes` should be moved to `folly/rust` (`folly/rust` should be just pure bindings to folly C++). Switching (making `minibytes` depend on `iobuf`) is annoying too, partially because there is no `Cargo.toml` for `iobuf` right now. So let's make it possible to construct `IOBufShared` without explicitly `unsafe impl BytesToIOufSharedExt`. Reviewed By: muirdm Differential Revision: D79678311 fbshipit-source-id: f79ea3c5312908ee26887f49c88cb2d91fe28b58
1 parent 192a081 commit b1b0d26

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

folly/rust/iobuf/src/iobuf.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ where
175175
// with `iobuf_take_ownership`. We box it up so that we can pass it to
176176
// a freeing function, called by C++ when it wants to release the memory.
177177
fn from(bytes: T) -> Self {
178+
// safety: `T` satisfies the contract of `from_owner` because the `BytesToIOBufSharedExt`
179+
// trait bound.
180+
unsafe { Self::from_owner(bytes) }
181+
}
182+
}
183+
184+
impl IOBufShared {
185+
/// This takes the Bytes, boxes it up, and passes ownership to the IOBuf
186+
/// with `iobuf_take_ownership`. We box it up so that we can pass it to
187+
/// a freeing function, called by C++ when it wants to release the memory.
188+
///
189+
/// SAFETY: Similar to `BytesToIOBufSharedExt`, `T` must ensure that its
190+
/// deref's slice is valid for the lifetime of `T`.
191+
pub unsafe fn from_owner<T: std::ops::Deref<Target = [u8]>>(bytes: T) -> Self {
178192
let len = bytes.len();
179193
let b = Box::new(bytes);
180194

0 commit comments

Comments
 (0)