BurntSushi / rust-snappy

Snappy compression implemented in Rust (including the Snappy frame format).
BSD 3-Clause "New" or "Revised" License
444 stars 43 forks source link

Reader/Writer inner get_mut() #24

Closed z64 closed 4 years ago

z64 commented 5 years ago

flate2 provides get_mut(); obtaining an inner mutable reference can be helpful when using a reader/writer over a streaming protocol where the buffer size would be infinite.

The current access to the low level Encoder/Decoder is great, and can be used to work around the fact that I can't manage the internal writer/reader "mid-flight" - however it shifts a lot of responsibility onto the developer to reatain the Reader/Writer block semantics (checksums, etc) - providing a "I know what I'm doing" get_mut() function would go a long way to support this as you can imagine.

Does this make sense? Or maybe there's something I missed? I can try to provide a more descriptive use case / sample code if necessary.

(Thank you for this awesome crate!)

BurntSushi commented 5 years ago

Yes, I think so. Mind submitting a PR to make sure we're on the same page? Should be fine though.

z64 commented 5 years ago

Cool. Yeah, I can try to throw something together this EST evening.

In brief, the most basic case is taking something like

let snap_read = Snap::Reader::new(Cursor::new(Vec::new());

// ..

let inner = snap_read.get_mut();
inner.write(some_snappy_frame_on_hand);
inner.set_position = 0;

// ..

let data = snap_read.read(some_buf)

You can imagine something like a websocket protocol that sends me individual (or multiple) snappy frames in a single binary websocket frame. I want my Client to have an inner Snap::Reader, whose buffer I can write to until I've received a complete message and then decompress it at once to release to data to the rest of the app, without consuming the Reader or duplicating code that reads the frame type / checksum handling.