From d0bdd792949885f79669986348c188de126dc8ce Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Sat, 13 Jan 2024 22:04:29 -0500 Subject: [PATCH] add test coverage for NoAllocBufferSegments::from_buffer --- .../src/serialize/no_alloc_buffer_segments.rs | 4 +-- .../tests/serialize_read_message_no_alloc.rs | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/capnp/src/serialize/no_alloc_buffer_segments.rs b/capnp/src/serialize/no_alloc_buffer_segments.rs index 3466ced84..46db9961c 100644 --- a/capnp/src/serialize/no_alloc_buffer_segments.rs +++ b/capnp/src/serialize/no_alloc_buffer_segments.rs @@ -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 { let segment_table_info = read_segment_table(slice, options)?; diff --git a/capnp/tests/serialize_read_message_no_alloc.rs b/capnp/tests/serialize_read_message_no_alloc.rs index ed49783c6..64dec8ba7 100644 --- a/capnp/tests/serialize_read_message_no_alloc.rs +++ b/capnp/tests/serialize_read_message_no_alloc.rs @@ -26,3 +26,28 @@ pub fn serialize_read_message_no_alloc() { assert_eq!("hello world!", s); } } + +#[repr(C, align(8))] +struct BufferWrapper { + bytes: [u8; N], +} + +impl AsRef<[u8]> for BufferWrapper { + 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::().unwrap(); + assert_eq!(t, "abcdefg"); +}