Skip to content

Commit 9738fed

Browse files
committed
simplify NoAllocBufferSegmentType::SingleSegment
1 parent bcaac56 commit 9738fed

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

capnp/src/serialize/no_alloc_buffer_segments.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ fn read_segment_table(slice: &[u8], options: ReaderOptions) -> Result<ReadSegmen
8585
pub type NoAllocSliceSegments<'b> = NoAllocBufferSegments<&'b [u8]>;
8686

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

9493
/// The buffer contains multiple segments. In this case, the segment table
9594
/// needs to be re-parsed on each call to `get_segment()`.
@@ -114,12 +113,10 @@ pub struct NoAllocBufferSegments<T> {
114113
impl<T> NoAllocBufferSegments<T> {
115114
pub(crate) fn from_segment_table(buffer: T, info: ReadSegmentTableResult) -> Self {
116115
if info.segments_count == 1 {
117-
let message_length = info.segment_table_length_bytes + info.total_segments_length_bytes;
118116
Self {
119117
buffer,
120118
segment_type: NoAllocBufferSegmentType::SingleSegment(
121-
info.segment_table_length_bytes,
122-
message_length,
119+
info.total_segments_length_bytes,
123120
),
124121
}
125122
} else {
@@ -172,9 +169,9 @@ impl<T: AsRef<[u8]>> ReaderSegments for NoAllocBufferSegments<T> {
172169
let idx: usize = idx.try_into().unwrap();
173170

174171
match self.segment_type {
175-
NoAllocBufferSegmentType::SingleSegment(start, end) => {
172+
NoAllocBufferSegmentType::SingleSegment(length_bytes) => {
176173
if idx == 0 {
177-
Some(&self.buffer.as_ref()[start..end])
174+
Some(&self.buffer.as_ref()[8..8 + length_bytes])
178175
} else {
179176
None
180177
}

0 commit comments

Comments
 (0)