Skip to content

Commit

Permalink
SharedBytesBuilder::apply_unfilled and read_buf feature
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLuq committed Oct 7, 2023
1 parent 37c6a98 commit 55fc195
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"rust-analyzer.cargo.features": "all",
"rust-analyzer.check.features": "all"
}
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ alloc = ["serde_1?/alloc"]
bytes_1_safe = ["bytes_1"]
http-body_04 = ["dep:http-body_04", "dep:http_02", "bytes_1"]
std = ["alloc"]
nightly = []
read_buf = ["std"]

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(all(feature = "nightly", feature = "read_buf"), feature(read_buf))]
#![deny(missing_docs)]

#[cfg(feature = "alloc")]
Expand Down
2 changes: 1 addition & 1 deletion src/shared_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl core::hash::Hash for SharedBytes {
impl PartialOrd for SharedBytes {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.as_slice().partial_cmp(other.as_slice())
Some(self.as_slice().cmp(other.as_slice()))
}
}

Expand Down
50 changes: 48 additions & 2 deletions src/shared_bytes_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ impl SharedBytesBuilder {
}

/// Returns the number of bytes currently in the buffer.
#[inline]
pub const fn len(&self) -> usize {
self.off as usize - 8
}

/// Returns `true` if the buffer is empty.
#[inline]
pub const fn is_empty(&self) -> bool {
self.off == 8
}
Expand All @@ -176,7 +178,7 @@ impl SharedBytesBuilder {
return &[];
}
unsafe {
core::slice::from_raw_parts(self.dat.offset(self.off as isize), self.off as usize - 8)
core::slice::from_raw_parts(self.dat.offset(8), self.off as usize - 8)
}
}

Expand All @@ -187,11 +189,34 @@ impl SharedBytesBuilder {
}
unsafe {
core::slice::from_raw_parts_mut(
self.dat.offset(self.off as isize),
self.dat.offset(8),
self.off as usize - 8,
)
}
}

/// Apply a function to the unused reserved bytes.
///
/// The function is passed a mutable slice of `MaybeUninit<u8>` and returns a tuple of the return value and the number of bytes filled.
pub fn apply_unfilled<R, F>(&mut self, f: F) -> R
where F: FnOnce(&mut [core::mem::MaybeUninit<u8>]) -> (R, usize),
{
let off = self.off as isize;
let data = if off == 8 {
&mut [] as &mut [core::mem::MaybeUninit<u8>]
} else {
unsafe {
core::slice::from_raw_parts_mut(
self.dat.offset(off) as *mut core::mem::MaybeUninit<u8>,
self.len as usize - off as usize,
)
}
};
let (ret, len) = f(data);
assert!(len <= data.len());
self.len += len as u32;
ret
}
}

impl Default for SharedBytesBuilder {
Expand Down Expand Up @@ -308,3 +333,24 @@ impl core::fmt::UpperHex for SharedBytesBuilder {
Ok(())
}
}

#[cfg(feature = "read_buf")]
impl SharedBytesBuilder {
/// Apply a function to the unused reserved bytes.
pub fn apply_borrowed_buf<'this, R, F>(&'this mut self, f: F) -> R
where F: FnOnce(&mut std::io::BorrowedBuf<'this>) -> R,
{
let off = self.off as isize;
let mut bb = if off == 8 {
std::io::BorrowedBuf::from(&mut [] as &mut [u8])
} else {
let data = unsafe { self.dat.offset(off) as *mut core::mem::MaybeUninit<u8> };
let data = unsafe { core::slice::from_raw_parts_mut(data, self.len as usize - off as usize) };
std::io::BorrowedBuf::from(data)
};
let ret = f(&mut bb);
let len = bb.len() as u32;
self.len += len;
ret
}
}

0 comments on commit 55fc195

Please sign in to comment.