Skip to content

Commit cb51567

Browse files
committed
auto merge of #18788 : ricky26/rust/master, r=aturon
This moves chars() and lines() out of Buffer and into separate traits (CharsBuffer and LinesBuffer respectively) - this matches the pattern used for bytes() on Reader (with BytesReader). (I came across this when I wanted a trait object of a Buffer, so that I could use read_line(); rustc errors about std::io::Buffer not being object-safe.) [breaking-change] Any uses of Buffer::lines() will need to use the new trait std::io::LinesBuffer. The same is true for Buffer::chars() with std::io::CharsBuffer.
2 parents 7e43f41 + 43fd644 commit cb51567

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/libstd/io/mod.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,16 +1437,6 @@ pub trait Buffer: Reader {
14371437
)
14381438
}
14391439

1440-
/// Create an iterator that reads a line on each iteration until EOF.
1441-
///
1442-
/// # Error
1443-
///
1444-
/// Any error other than `EndOfFile` that is produced by the underlying Reader
1445-
/// is returned by the iterator and should be handled by the caller.
1446-
fn lines<'r>(&'r mut self) -> Lines<'r, Self> {
1447-
Lines { buffer: self }
1448-
}
1449-
14501440
/// Reads a sequence of bytes leading up to a specified delimiter. Once the
14511441
/// specified byte is encountered, reading ceases and the bytes up to and
14521442
/// including the delimiter are returned.
@@ -1522,17 +1512,36 @@ pub trait Buffer: Reader {
15221512
None => Err(standard_error(InvalidInput))
15231513
}
15241514
}
1515+
}
15251516

1517+
/// Extension methods for the Buffer trait which are included in the prelude.
1518+
pub trait BufferPrelude {
15261519
/// Create an iterator that reads a utf8-encoded character on each iteration
15271520
/// until EOF.
15281521
///
15291522
/// # Error
15301523
///
15311524
/// Any error other than `EndOfFile` that is produced by the underlying Reader
15321525
/// is returned by the iterator and should be handled by the caller.
1533-
fn chars<'r>(&'r mut self) -> Chars<'r, Self> {
1526+
fn chars<'r>(&'r mut self) -> Chars<'r, Self>;
1527+
1528+
/// Create an iterator that reads a line on each iteration until EOF.
1529+
///
1530+
/// # Error
1531+
///
1532+
/// Any error other than `EndOfFile` that is produced by the underlying Reader
1533+
/// is returned by the iterator and should be handled by the caller.
1534+
fn lines<'r>(&'r mut self) -> Lines<'r, Self>;
1535+
}
1536+
1537+
impl<T: Buffer> BufferPrelude for T {
1538+
fn chars<'r>(&'r mut self) -> Chars<'r, T> {
15341539
Chars { buffer: self }
15351540
}
1541+
1542+
fn lines<'r>(&'r mut self) -> Lines<'r, T> {
1543+
Lines { buffer: self }
1544+
}
15361545
}
15371546

15381547
/// When seeking, the resulting cursor is offset from a base by the offset given
@@ -2003,4 +2012,8 @@ mod tests {
20032012
assert_eq!(format!("{}", ALL_PERMISSIONS), "0777".to_string());
20042013
assert_eq!(format!("{}", USER_READ | USER_WRITE | OTHER_WRITE), "0602".to_string());
20052014
}
2015+
2016+
fn _ensure_buffer_is_object_safe<T: Buffer>(x: &T) -> &Buffer {
2017+
x as &Buffer
2018+
}
20062019
}

src/libstd/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
#[doc(no_inline)] pub use path::{GenericPath, Path, PosixPath, WindowsPath};
7474
#[doc(no_inline)] pub use ptr::{RawPtr, RawMutPtr};
7575
#[doc(no_inline)] pub use result::{Result, Ok, Err};
76-
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek};
76+
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek, BufferPrelude};
7777
#[doc(no_inline)] pub use str::{Str, StrVector, StrPrelude};
7878
#[doc(no_inline)] pub use str::{IntoMaybeOwned, StrAllocating, UnicodeStrPrelude};
7979
#[doc(no_inline)] pub use to_string::{ToString, IntoStr};

0 commit comments

Comments
 (0)