Skip to content

capnp: adding capnp::serialize::NoAllocSliceSegments and capnp::serialize::read_message_from_flat_slice_no_alloc #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion capnp/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
//! [standard stream framing](https://capnproto.org/encoding.html#serialization-over-a-stream),
//! where each message is preceded by a segment table indicating the size of its segments.

mod no_alloc_slice_segments;
pub use no_alloc_slice_segments::NoAllocSliceSegments;

use alloc::string::ToString;
use alloc::vec::Vec;
use core::convert::TryInto;
Expand All @@ -33,6 +36,7 @@ use crate::message;
use crate::private::units::BYTES_PER_WORD;
use crate::{Error, Result};

pub const SEGMENTS_COUNT_LIMIT : usize = 512;

/// Segments read from a single flat slice of words.
pub struct SliceSegments<'a> {
Expand Down Expand Up @@ -88,6 +92,22 @@ pub fn read_message_from_flat_slice<'a>(slice: &mut &'a [u8],
}
}

/// Reads a serialized message (including a segment table) from a flat slice of bytes, without copying.
/// The slice 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.
///
/// Unlike read_message_from_flat_slice_no_alloc it does not do heap allocation (except for error message)
///
/// 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 read_message_from_flat_slice_no_alloc<'a>(slice: &mut &'a [u8],
options: message::ReaderOptions)
-> Result<message::Reader<NoAllocSliceSegments<'a>>> {
let segments = NoAllocSliceSegments::try_new(slice, options)?;

Ok(message::Reader::new(segments, options))
}

/// Segments read from a buffer, useful for when you have the message in a buffer and don't want the extra
/// copy of `read_message`.
pub struct BufferSegments<T> {
Expand Down Expand Up @@ -291,7 +311,7 @@ fn read_segment_table<R>(read: &mut R,

let segment_count = u32::from_le_bytes(buf[0..4].try_into().unwrap()).wrapping_add(1) as usize;

if segment_count >= 512 {
if segment_count >= SEGMENTS_COUNT_LIMIT {
return Err(Error::failed(format!("Too many segments: {}", segment_count)))
} else if segment_count == 0 {
return Err(Error::failed(format!("Too few segments: {}", segment_count)))
Expand Down
Loading