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 ReadWrapper and WriteWrapper traits #32625

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 47 additions & 0 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,53 @@ pub trait Write {
fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
}

/// A trait for objects which are wrap byte-oriented reader.
///
/// This wrappers typically used for make some data transformation such as
/// decompression or data decryption.
///
/// ReadWrapper are defined two additional required methods, `reader()` and `finish()`:
///
/// * The `reader()` method will allow access to wrapped object.
///
/// * The `finish()` method complete reading and unwrap original reader.
/// This method MAY NOT unwrap original stream until reached end of stream by `read()`
/// method.
pub trait ReadWrapper<R: Read>: Read {
/// Get wrapped object.
fn reader(&self) -> &R;

/// Complete reading and unwrap original reader.
///
/// This method MAY NOT unwrap original stream until reached end of stream by `read()`
/// method.
fn finish(self) -> (R, Result<()>);
}

/// A trait for objects which are wrap byte-oriented writer.
///
/// This wrappers typically used for make some data transformation such as
/// compression or data encryption.
///
/// WriteWrapper are defined two additional required methods, `writer()` and `finish()`:
///
/// * The `writer()` method will allow access to wrapped object.
///
/// * The `finish()` method complete writing (usually it write end of stream mark) and
/// unwrap original writer.
/// This method MUST be called on successfully code branch.
///
/// WriteWrapper SHOULD NOT write some data to wrapped writer in drop method.
/// For example, for compression wrapped we do not want to get a "correct" stream without
/// explict `finish()` call.
pub trait WriteWrapper<W: Write>: Write {
/// Get wrapped object.
fn writer(&self) -> &W;

/// Complete writing (usually it write end of stream mark) and unwrap original writer.
fn finish(self) -> (W, Result<()>);
}

/// The `Seek` trait provides a cursor which can be moved within a stream of
/// bytes.
///
Expand Down