Skip to content

Commit

Permalink
simplify NoAllocBufferSegmentType::SingleSegment
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrensha committed Mar 28, 2024
1 parent bcaac56 commit 9738fed
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions capnp/src/serialize/no_alloc_buffer_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ fn read_segment_table(slice: &[u8], options: ReaderOptions) -> Result<ReadSegmen
pub type NoAllocSliceSegments<'b> = NoAllocBufferSegments<&'b [u8]>;

enum NoAllocBufferSegmentType {
/// The buffer contains a single segment, with bounds given by the two
/// `usize` parameters. The first parameter gives the byte offset of the
/// start of the segment, and the second parameter gives the byte offset
/// of its end.
SingleSegment(usize, usize),
/// The buffer contains a single segment, with length in bytes given by
/// the value of the `usize` parameter. The segment starts at byte index
/// 8 of the buffer.
SingleSegment(usize),

/// The buffer contains multiple segments. In this case, the segment table
/// needs to be re-parsed on each call to `get_segment()`.
Expand All @@ -114,12 +113,10 @@ pub struct NoAllocBufferSegments<T> {
impl<T> NoAllocBufferSegments<T> {
pub(crate) fn from_segment_table(buffer: T, info: ReadSegmentTableResult) -> Self {
if info.segments_count == 1 {
let message_length = info.segment_table_length_bytes + info.total_segments_length_bytes;
Self {
buffer,
segment_type: NoAllocBufferSegmentType::SingleSegment(
info.segment_table_length_bytes,
message_length,
info.total_segments_length_bytes,
),
}
} else {
Expand Down Expand Up @@ -172,9 +169,9 @@ impl<T: AsRef<[u8]>> ReaderSegments for NoAllocBufferSegments<T> {
let idx: usize = idx.try_into().unwrap();

match self.segment_type {
NoAllocBufferSegmentType::SingleSegment(start, end) => {
NoAllocBufferSegmentType::SingleSegment(length_bytes) => {
if idx == 0 {
Some(&self.buffer.as_ref()[start..end])
Some(&self.buffer.as_ref()[8..8 + length_bytes])
} else {
None
}
Expand Down

0 comments on commit 9738fed

Please sign in to comment.