Skip to content

Commit

Permalink
add test coverage for NoAllocBufferSegments::from_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Jan 14, 2024
1 parent 0cf983d commit d0bdd79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions capnp/src/serialize/no_alloc_buffer_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ impl<'b> NoAllocBufferSegments<&'b [u8]> {
/// The buffer is allowed to extend beyond the end of the message. On success, updates `slice` to point
/// to the remaining bytes beyond the end of the message.
///
/// ALIGNMENT: If the "unaligned" feature is enabled, then there are no alignment requirements on `buffer`.
/// Otherwise, `buffer` must be 8-byte aligned (attempts to read the message will trigger errors).
/// ALIGNMENT: If the "unaligned" feature is enabled, then there are no alignment requirements on `slice`.
/// Otherwise, `slice` must be 8-byte aligned (attempts to read the message will trigger errors).
pub fn from_slice(slice: &mut &'b [u8], options: ReaderOptions) -> Result<Self> {
let segment_table_info = read_segment_table(slice, options)?;

Expand Down
25 changes: 25 additions & 0 deletions capnp/tests/serialize_read_message_no_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,28 @@ pub fn serialize_read_message_no_alloc() {
assert_eq!("hello world!", s);
}
}

#[repr(C, align(8))]
struct BufferWrapper<const N: usize> {
bytes: [u8; N],
}

impl<const N: usize> AsRef<[u8]> for BufferWrapper<N> {
fn as_ref(&self) -> &[u8] {
&self.bytes[..]
}
}

#[test]
pub fn no_alloc_buffer_segments_from_buffer() {
let buffer = BufferWrapper {
bytes: [
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x42, 0x00,
0x00, 0x00, 97, 98, 99, 100, 101, 102, 103, 0, // "abcdefg" with null terminator
],
};
let segs = serialize::NoAllocBufferSegments::from_buffer(buffer, Default::default()).unwrap();
let message = message::Reader::new(segs, Default::default());
let t = message.get_root::<capnp::text::Reader>().unwrap();
assert_eq!(t, "abcdefg");
}

0 comments on commit d0bdd79

Please sign in to comment.