Skip to content

Instantly share code, notes, and snippets.

@robey
Created March 7, 2017 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robey/7464dd437d20f8ea7315be775207b969 to your computer and use it in GitHub Desktop.
Save robey/7464dd437d20f8ea7315be775207b969 to your computer and use it in GitHub Desktop.
type ByteStream = Stream<Item = EasyBuf, Error = io::Error>;
// pub fn framed_stream<T>(s: T) -> impl T
// where T: Stream<Item = EasyBuf, Error = io::Error>
// convert a byte stream into a stream with each chunk prefixed by a length
// marker, suitable for embedding in a bottle.
pub fn framed_stream<T>(s: T) -> stream::Flatten<
stream::Map<T, fn(EasyBuf) -> stream::IterStream<vec::IntoIter<Result<EasyBuf, io::Error>>>>
>
where T: Stream<Item = EasyBuf, Error = io::Error>
{
fn frame(buffer: EasyBuf) -> stream::IterStream<vec::IntoIter<Result<EasyBuf, io::Error>>> {
stream::iter(vec![ Ok(EasyBuf::from(zint::encode_length(buffer.len() as u32))), Ok(buffer) ])
}
let x = s.map(frame as fn(EasyBuf) -> stream::IterStream<vec::IntoIter<Result<EasyBuf, io::Error>>>);
x.flatten()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment