Skip to content
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

Add read_from_packed_stream to make reading individual packed messages easier #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions examples/addressbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,8 +52,7 @@ pub fn write_address_book<W: Write>(target: &mut W) {
}

pub fn print_address_book<W: Write>(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::<AddressBook>();

Expand Down
5 changes: 2 additions & 3 deletions fuzz/fuzz_targets/unpacking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
18 changes: 17 additions & 1 deletion recapn/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,24 @@ impl<R: io::BufRead> PackedStream<R> {
}
}

/// 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<R: io::BufRead>(
#[inline]
pub fn read_from_packed_stream<R: io::BufRead>(
stream: R,
options: StreamOptions,
) -> Result<SegmentSet<Box<[Word]>>, 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<R: io::BufRead>(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find name difference very insignificant and as a result I have no idea which one to use based on the name.
Can we differentiate these two more? Maybe with doesn't need to be pub?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention is that "with" implies you're using an existing thing, "from" implies you're consuming the thing completely. "with" needs to be pub because you could have a stream of multiple packed messages or general packed data. You want to keep the PackedStream state around so you can read more stuff.

stream: &mut PackedStream<R>,
options: StreamOptions,
) -> Result<SegmentSet<Box<[Word]>>, StreamError> {
Expand Down