Skip to content

Make std::io::Buffer object-safe. #18788

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

Merged
merged 1 commit into from
Nov 16, 2014
Merged
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
35 changes: 24 additions & 11 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,16 +1402,6 @@ pub trait Buffer: Reader {
)
}

/// Create an iterator that reads a line on each iteration until EOF.
///
/// # Error
///
/// Any error other than `EndOfFile` that is produced by the underlying Reader
/// is returned by the iterator and should be handled by the caller.
fn lines<'r>(&'r mut self) -> Lines<'r, Self> {
Lines { buffer: self }
}

/// Reads a sequence of bytes leading up to a specified delimiter. Once the
/// specified byte is encountered, reading ceases and the bytes up to and
/// including the delimiter are returned.
Expand Down Expand Up @@ -1487,17 +1477,36 @@ pub trait Buffer: Reader {
None => Err(standard_error(InvalidInput))
}
}
}

/// Extension methods for the Buffer trait which are included in the prelude.
pub trait BufferPrelude {
/// Create an iterator that reads a utf8-encoded character on each iteration
/// until EOF.
///
/// # Error
///
/// Any error other than `EndOfFile` that is produced by the underlying Reader
/// is returned by the iterator and should be handled by the caller.
fn chars<'r>(&'r mut self) -> Chars<'r, Self> {
fn chars<'r>(&'r mut self) -> Chars<'r, Self>;

/// Create an iterator that reads a line on each iteration until EOF.
///
/// # Error
///
/// Any error other than `EndOfFile` that is produced by the underlying Reader
/// is returned by the iterator and should be handled by the caller.
fn lines<'r>(&'r mut self) -> Lines<'r, Self>;
}

impl<T: Buffer> BufferPrelude for T {
fn chars<'r>(&'r mut self) -> Chars<'r, T> {
Chars { buffer: self }
}

fn lines<'r>(&'r mut self) -> Lines<'r, T> {
Lines { buffer: self }
}
}

/// When seeking, the resulting cursor is offset from a base by the offset given
Expand Down Expand Up @@ -1968,4 +1977,8 @@ mod tests {
assert_eq!(format!("{}", ALL_PERMISSIONS), "0777".to_string());
assert_eq!(format!("{}", USER_READ | USER_WRITE | OTHER_WRITE), "0602".to_string());
}

fn _ensure_buffer_is_object_safe<T: Buffer>(x: &T) -> &Buffer {
x as &Buffer
}
}
2 changes: 1 addition & 1 deletion src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#[doc(no_inline)] pub use path::{GenericPath, Path, PosixPath, WindowsPath};
#[doc(no_inline)] pub use ptr::{RawPtr, RawMutPtr};
#[doc(no_inline)] pub use result::{Result, Ok, Err};
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek};
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek, BufferPrelude};
#[doc(no_inline)] pub use str::{Str, StrVector, StrPrelude};
#[doc(no_inline)] pub use str::{IntoMaybeOwned, StrAllocating, UnicodeStrPrelude};
#[doc(no_inline)] pub use to_string::{ToString, IntoStr};
Expand Down