Skip to content

Commit 43fd644

Browse files
committed
Make std::io::Buffer object-safe.
[breaking-change] Any uses of Buffer::lines() and Buffer::chars() will need to use the new trait std::io::BufferPrelude.
1 parent 15ba87f commit 43fd644

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/libstd/io/mod.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -1402,16 +1402,6 @@ pub trait Buffer: Reader {
14021402
)
14031403
}
14041404

1405-
/// Create an iterator that reads a line on each iteration until EOF.
1406-
///
1407-
/// # Error
1408-
///
1409-
/// Any error other than `EndOfFile` that is produced by the underlying Reader
1410-
/// is returned by the iterator and should be handled by the caller.
1411-
fn lines<'r>(&'r mut self) -> Lines<'r, Self> {
1412-
Lines { buffer: self }
1413-
}
1414-
14151405
/// Reads a sequence of bytes leading up to a specified delimiter. Once the
14161406
/// specified byte is encountered, reading ceases and the bytes up to and
14171407
/// including the delimiter are returned.
@@ -1487,17 +1477,36 @@ pub trait Buffer: Reader {
14871477
None => Err(standard_error(InvalidInput))
14881478
}
14891479
}
1480+
}
14901481

1482+
/// Extension methods for the Buffer trait which are included in the prelude.
1483+
pub trait BufferPrelude {
14911484
/// Create an iterator that reads a utf8-encoded character on each iteration
14921485
/// until EOF.
14931486
///
14941487
/// # Error
14951488
///
14961489
/// Any error other than `EndOfFile` that is produced by the underlying Reader
14971490
/// is returned by the iterator and should be handled by the caller.
1498-
fn chars<'r>(&'r mut self) -> Chars<'r, Self> {
1491+
fn chars<'r>(&'r mut self) -> Chars<'r, Self>;
1492+
1493+
/// Create an iterator that reads a line on each iteration until EOF.
1494+
///
1495+
/// # Error
1496+
///
1497+
/// Any error other than `EndOfFile` that is produced by the underlying Reader
1498+
/// is returned by the iterator and should be handled by the caller.
1499+
fn lines<'r>(&'r mut self) -> Lines<'r, Self>;
1500+
}
1501+
1502+
impl<T: Buffer> BufferPrelude for T {
1503+
fn chars<'r>(&'r mut self) -> Chars<'r, T> {
14991504
Chars { buffer: self }
15001505
}
1506+
1507+
fn lines<'r>(&'r mut self) -> Lines<'r, T> {
1508+
Lines { buffer: self }
1509+
}
15011510
}
15021511

15031512
/// When seeking, the resulting cursor is offset from a base by the offset given
@@ -1968,4 +1977,8 @@ mod tests {
19681977
assert_eq!(format!("{}", ALL_PERMISSIONS), "0777".to_string());
19691978
assert_eq!(format!("{}", USER_READ | USER_WRITE | OTHER_WRITE), "0602".to_string());
19701979
}
1980+
1981+
fn _ensure_buffer_is_object_safe<T: Buffer>(x: &T) -> &Buffer {
1982+
x as &Buffer
1983+
}
19711984
}

src/libstd/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
#[doc(no_inline)] pub use path::{GenericPath, Path, PosixPath, WindowsPath};
7676
#[doc(no_inline)] pub use ptr::{RawPtr, RawMutPtr};
7777
#[doc(no_inline)] pub use result::{Result, Ok, Err};
78-
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek};
78+
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek, BufferPrelude};
7979
#[doc(no_inline)] pub use str::{Str, StrVector, StrPrelude};
8080
#[doc(no_inline)] pub use str::{IntoMaybeOwned, StrAllocating, UnicodeStrPrelude};
8181
#[doc(no_inline)] pub use to_string::{ToString, IntoStr};

0 commit comments

Comments
 (0)