about summary refs log tree commit diff
path: root/src/libstd/io
diff options
context:
space:
mode:
authorRicky Taylor <rickytaylor26@gmail.com>2014-11-08 21:59:09 +0000
committerRicky Taylor <rickytaylor26@gmail.com>2014-11-13 22:38:39 +0000
commit43fd6446add0bcf237f3fe15b7d715eceee2a08c (patch)
tree6522fd8e643ba0ac20d1a0869da0de95766df24e /src/libstd/io
parent15ba87f0314fda5e81603f37ae5f40e2022bcfc1 (diff)
downloadrust-43fd6446add0bcf237f3fe15b7d715eceee2a08c.tar.gz
rust-43fd6446add0bcf237f3fe15b7d715eceee2a08c.zip
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.
Diffstat (limited to 'src/libstd/io')
-rw-r--r--src/libstd/io/mod.rs35
1 files changed, 24 insertions, 11 deletions
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs
index 31eab4363d0..54197ab2e01 100644
--- a/src/libstd/io/mod.rs
+++ b/src/libstd/io/mod.rs
@@ -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.
@@ -1487,7 +1477,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.
     ///
@@ -1495,9 +1488,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
@@ -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
+    }
 }