File tree Expand file tree Collapse file tree 3 files changed +23
-6
lines changed
Expand file tree Collapse file tree 3 files changed +23
-6
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 ( )
Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments