Skip to content

Commit

Permalink
buf: impl IoBuf/IoBufMut for bytes::Bytes/BytesMut
Browse files Browse the repository at this point in the history
import bytes as optional dependency.

Fixes #42
  • Loading branch information
songzhi committed Aug 9, 2021
1 parent fe681bf commit 4d9e990
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ keywords = ["async", "fs", "io-uring"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = []
bytes-trait = ["bytes"]

[dependencies]
tokio = { version = "1.2", features = ["net", "rt"] }
scoped-tls = "1.0.0"
slab = "0.4.2"
libc = "0.2.80"
io-uring = { version = "0.5.0", features = [ "unstable" ] }
bytes = { version = "1.0", optional = true }

[dev-dependencies]
bencher = "0.1.5"
Expand Down
30 changes: 30 additions & 0 deletions src/buf/io_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,33 @@ unsafe impl IoBuf for &'static str {
self.bytes_init()
}
}

#[cfg(feature = "bytes-trait")]
unsafe impl IoBuf for bytes::Bytes {
fn stable_ptr(&self) -> *const u8 {
self.as_ptr()
}

fn bytes_init(&self) -> usize {
self.len()
}

fn bytes_total(&self) -> usize {
self.len()
}
}

#[cfg(feature = "bytes-trait")]
unsafe impl IoBuf for bytes::BytesMut {
fn stable_ptr(&self) -> *const u8 {
self.as_ptr()
}

fn bytes_init(&self) -> usize {
self.len()
}

fn bytes_total(&self) -> usize {
self.capacity()
}
}
13 changes: 13 additions & 0 deletions src/buf/io_buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ unsafe impl IoBufMut for Vec<u8> {
}
}
}

#[cfg(feature = "bytes-trait")]
unsafe impl IoBufMut for bytes::BytesMut {
fn stable_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}

unsafe fn set_init(&mut self, init_len: usize) {
if self.len() < init_len {
self.set_len(init_len);
}
}
}

0 comments on commit 4d9e990

Please sign in to comment.