about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-11-16 04:37:36 +0000
committerbors <bors@rust-lang.org>2014-11-16 04:37:36 +0000
commitcb51567e1911452f3da4b924a91e5360f5efe67c (patch)
tree6491b2030f478bcc9a7da9db59da40c9dae4b651 /src/libstd
parent7e43f419cb98c9035d30e5cf0e0be7944dbc0371 (diff)
parent43fd6446add0bcf237f3fe15b7d715eceee2a08c (diff)
downloadrust-cb51567e1911452f3da4b924a91e5360f5efe67c.tar.gz
rust-cb51567e1911452f3da4b924a91e5360f5efe67c.zip
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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/io/mod.rs35
-rw-r--r--src/libstd/prelude.rs2
2 files changed, 25 insertions, 12 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs
index 64fed1a5442..90345cb0535 100644
--- a/src/libstd/io/mod.rs
+++ b/src/libstd/io/mod.rs
@@ -1437,16 +1437,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.
@@ -1522,7 +1512,10 @@ 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.
     ///
@@ -1530,9 +1523,25 @@ pub trait Buffer: Reader {
     ///
     /// 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
@@ -2003,4 +2012,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
+    }
 }
diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs
index 158e7a59f6d..e9793ea65ad 100644
--- a/src/libstd/prelude.rs
+++ b/src/libstd/prelude.rs
@@ -73,7 +73,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};