diff --git a/examples/addressbook/src/lib.rs b/examples/addressbook/src/lib.rs index a8e8339..4d71bef 100644 --- a/examples/addressbook/src/lib.rs +++ b/examples/addressbook/src/lib.rs @@ -5,7 +5,7 @@ pub mod gen { use std::io::Write; use gen::addressbook_capnp::*; -use recapn::io::{read_packed_from_stream, PackedStream, StreamOptions}; +use recapn::io::{self, StreamOptions}; use recapn::message::{self, Message, ReaderOptions}; use recapn::text; @@ -52,8 +52,7 @@ pub fn write_address_book(target: &mut W) { } pub fn print_address_book(src: &[u8], output: &mut W) -> ::recapn::Result<()> { - let mut message_reader = PackedStream::new(src); - let segments = read_packed_from_stream(&mut message_reader, StreamOptions::default()).unwrap(); + let segments = io::read_from_packed_stream(src, StreamOptions::default()).unwrap(); let message = message::Reader::new(&segments, ReaderOptions::default()); let address_book = message.read_as_struct::(); diff --git a/fuzz/fuzz_targets/unpacking.rs b/fuzz/fuzz_targets/unpacking.rs index 2e485f8..d8b1e53 100644 --- a/fuzz/fuzz_targets/unpacking.rs +++ b/fuzz/fuzz_targets/unpacking.rs @@ -2,9 +2,8 @@ use libfuzzer_sys::fuzz_target; -use recapn::io::{PackedStream, StreamOptions}; +use recapn::io::StreamOptions; fuzz_target!(|data: &[u8]| { - let mut packed = PackedStream::new(data); - let _ = recapn::io::read_packed_from_stream(&mut packed, StreamOptions::default()); + let _ = recapn::io::read_from_packed_stream(data, StreamOptions::default()); }); diff --git a/recapn/src/io.rs b/recapn/src/io.rs index f80b7e8..62b1ee8 100644 --- a/recapn/src/io.rs +++ b/recapn/src/io.rs @@ -498,8 +498,24 @@ impl PackedStream { } } +/// Read a message from a stream in packed encoding. This creates a `PackedStream` and reads the +/// message, flushing the packed state at the end to make sure the message is complete. #[cfg(feature = "std")] -pub fn read_packed_from_stream( +#[inline] +pub fn read_from_packed_stream( + stream: R, + options: StreamOptions, +) -> Result>, StreamError> { + let mut packed = PackedStream::new(stream); + let msg = read_with_packed_stream(&mut packed, options)?; + packed.finish()?; + Ok(msg) +} + +/// Read a message from a `PackedStream`. This allows you to continue reading more packed +/// data after the stream and reuse the packed state. +#[cfg(feature = "std")] +pub fn read_with_packed_stream( stream: &mut PackedStream, options: StreamOptions, ) -> Result>, StreamError> {