Skip to content

Commit cb72fe8

Browse files
Merge pull request #44 from triblespace/codex/add-feature-gated-push-method-to-bufferp
Add zerocopy push for ByteBuffer
2 parents 4d15e8c + 8e6dabc commit cb72fe8

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- install `rustfmt` and the Kani verifier automatically via `cargo install`
77
- restore Kani proof best practices in `AGENTS.md` and note that proofs run via `verify.sh`
88
- limit Kani loop unwind by default and set per-harness bounds
9+
- `ByteBuffer::push` now accepts any `IntoBytes + Immutable` value when the
10+
`zerocopy` feature is enabled
911
- increase unwind for prefix/suffix overflow proofs
1012
- move weak reference and downcasting examples into module docs
1113
- expand module introduction describing use cases

src/buffer.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ impl<const ALIGN: usize> ByteBuffer<ALIGN> {
102102
self.cap = new_cap;
103103
}
104104

105-
/// Push a byte to the end of the buffer.
105+
/// Push data to the end of the buffer.
106+
#[cfg(not(feature = "zerocopy"))]
106107
pub fn push(&mut self, byte: u8) {
107108
self.reserve_more(1);
108109
unsafe {
@@ -111,6 +112,20 @@ impl<const ALIGN: usize> ByteBuffer<ALIGN> {
111112
self.len += 1;
112113
}
113114

115+
/// Push data to the end of the buffer.
116+
#[cfg(feature = "zerocopy")]
117+
pub fn push<T>(&mut self, value: T)
118+
where
119+
T: zerocopy::IntoBytes + zerocopy::Immutable,
120+
{
121+
let bytes = zerocopy::IntoBytes::as_bytes(&value);
122+
self.reserve_more(bytes.len());
123+
unsafe {
124+
ptr::copy_nonoverlapping(bytes.as_ptr(), self.ptr.as_ptr().add(self.len), bytes.len());
125+
}
126+
self.len += bytes.len();
127+
}
128+
114129
/// Returns a raw pointer to the buffer's memory.
115130
pub fn as_ptr(&self) -> *const u8 {
116131
self.ptr.as_ptr()

src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ fn test_bytebuffer_push_and_bytes() {
308308
use crate::ByteBuffer;
309309

310310
let mut buf: ByteBuffer<8> = ByteBuffer::with_capacity(2);
311-
buf.push(1);
312-
buf.push(2);
313-
buf.push(3);
311+
buf.push(1u8);
312+
buf.push(2u8);
313+
buf.push(3u8);
314314
assert_eq!(buf.as_ref(), &[1, 2, 3]);
315315

316316
let bytes: Bytes = buf.into();
@@ -322,7 +322,7 @@ fn test_bytebuffer_alignment() {
322322
use crate::ByteBuffer;
323323

324324
let mut buf: ByteBuffer<64> = ByteBuffer::with_capacity(1);
325-
buf.push(1);
325+
buf.push(1u8);
326326
assert_eq!((buf.as_ptr() as usize) % 64, 0);
327327
}
328328

@@ -334,7 +334,7 @@ fn test_bytebuffer_reserve_total() {
334334
buf.reserve_total(10);
335335
assert!(buf.capacity() >= 10);
336336
for _ in 0..10 {
337-
buf.push(1);
337+
buf.push(1u8);
338338
}
339339
assert_eq!(buf.len(), 10);
340340
assert!(buf.capacity() >= 10);

0 commit comments

Comments
 (0)