Skip to content

Commit

Permalink
give NoAllocSegmentTableInfo public visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Mar 29, 2024
1 parent cdc54a3 commit 20b1bfe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions capnp/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,13 @@ where
}
read.read_exact(&mut buffer[start..end])?;

let info = no_alloc_buffer_segments::ReadSegmentTableResult {
let info = no_alloc_buffer_segments::NoAllocSegmentTableInfo {
segments_count: segment_count,
segment_table_length_bytes: (num_segment_counts_read + 1) * 4,
total_segments_length_bytes: total_body_words * 8,
};

let segments = NoAllocSliceSegments::from_segment_table(buffer, info);
let segments = NoAllocSliceSegments::from_segment_table_info(buffer, info);
Ok(Some(crate::message::Reader::new(segments, options)))
}

Expand Down
22 changes: 16 additions & 6 deletions capnp/src/serialize/no_alloc_buffer_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ use super::SEGMENTS_COUNT_LIMIT;

const U32_LEN_IN_BYTES: usize = core::mem::size_of::<u32>();

pub(crate) struct ReadSegmentTableResult {
/// Information about a message read from its segment table.
pub struct NoAllocSegmentTableInfo {
/// The number of segments in the message.
pub segments_count: usize,

/// The number of bytes in the segment table.
pub segment_table_length_bytes: usize,

/// The total number of bytes in all segments of the message.
pub total_segments_length_bytes: usize,
}

fn read_segment_table(slice: &[u8], options: ReaderOptions) -> Result<ReadSegmentTableResult> {
fn read_segment_table(slice: &[u8], options: ReaderOptions) -> Result<NoAllocSegmentTableInfo> {
let mut remaining = slice;

verify_alignment(remaining.as_ptr())?;
Expand Down Expand Up @@ -74,7 +80,7 @@ fn read_segment_table(slice: &[u8], options: ReaderOptions) -> Result<ReadSegmen
)));
}

Ok(ReadSegmentTableResult {
Ok(NoAllocSegmentTableInfo {
segments_count,
segment_table_length_bytes: expected_data_offset,
total_segments_length_bytes,
Expand Down Expand Up @@ -111,7 +117,11 @@ pub struct NoAllocBufferSegments<T> {
}

impl<T> NoAllocBufferSegments<T> {
pub(crate) fn from_segment_table(buffer: T, info: ReadSegmentTableResult) -> Self {
/// Constructs a NoAllocBufferSegments from a buffer and a `NoAllocSegmentTableInfo`.
/// This method is used internally by `NoAllocBufferSegments::from_slice()`
/// and `NoAllocBufferSegments::from_buffer()`. It has been made public to allow for
/// situations where the segment table is read by nonstandard means.
pub fn from_segment_table_info(buffer: T, info: NoAllocSegmentTableInfo) -> Self {
if info.segments_count == 1 {
Self {
buffer,
Expand Down Expand Up @@ -144,7 +154,7 @@ impl<'b> NoAllocBufferSegments<&'b [u8]> {
let message = &slice[..message_length];
*slice = &slice[message_length..];

Ok(Self::from_segment_table(message, segment_table_info))
Ok(Self::from_segment_table_info(message, segment_table_info))
}
}

Expand All @@ -156,7 +166,7 @@ impl<T: AsRef<[u8]>> NoAllocBufferSegments<T> {
/// Otherwise, `buffer` must be 8-byte aligned (attempts to read the message will trigger errors).
pub fn from_buffer(buffer: T, options: ReaderOptions) -> Result<Self> {
let segment_table_info = read_segment_table(buffer.as_ref(), options)?;
Ok(Self::from_segment_table(buffer, segment_table_info))
Ok(Self::from_segment_table_info(buffer, segment_table_info))
}
}

Expand Down

0 comments on commit 20b1bfe

Please sign in to comment.