aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/io/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ctr-std/src/io/util.rs')
-rw-r--r--ctr-std/src/io/util.rs67
1 files changed, 57 insertions, 10 deletions
diff --git a/ctr-std/src/io/util.rs b/ctr-std/src/io/util.rs
index 2c68802..45d281e 100644
--- a/ctr-std/src/io/util.rs
+++ b/ctr-std/src/io/util.rs
@@ -10,7 +10,9 @@
#![allow(missing_copy_implementations)]
-use io::{self, Read, Write, ErrorKind, BufRead};
+use fmt;
+use io::{self, Read, Initializer, Write, ErrorKind, BufRead};
+use mem;
/// Copies the entire contents of a reader into a writer.
///
@@ -36,17 +38,23 @@ use io::{self, Read, Write, ErrorKind, BufRead};
/// let mut reader: &[u8] = b"hello";
/// let mut writer: Vec<u8> = vec![];
///
-/// try!(io::copy(&mut reader, &mut writer));
+/// io::copy(&mut reader, &mut writer)?;
///
-/// assert_eq!(reader, &writer[..]);
+/// assert_eq!(&b"hello"[..], &writer[..]);
/// # Ok(())
/// # }
+/// # foo().unwrap();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
where R: Read, W: Write
{
- let mut buf = [0; super::DEFAULT_BUF_SIZE];
+ let mut buf = unsafe {
+ let mut buf: [u8; super::DEFAULT_BUF_SIZE] = mem::uninitialized();
+ reader.initializer().initialize(&mut buf);
+ buf
+ };
+
let mut written = 0;
loop {
let len = match reader.read(&mut buf) {
@@ -62,16 +70,18 @@ pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<
/// A reader which is always at EOF.
///
-/// This struct is generally created by calling [`empty()`][empty]. Please see
-/// the documentation of `empty()` for more details.
+/// This struct is generally created by calling [`empty`]. Please see
+/// the documentation of [`empty()`][`empty`] for more details.
///
-/// [empty]: fn.empty.html
+/// [`empty`]: fn.empty.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Empty { _priv: () }
/// Constructs a new handle to an empty reader.
///
-/// All reads from the returned reader will return `Ok(0)`.
+/// All reads from the returned reader will return [`Ok`]`(0)`.
+///
+/// [`Ok`]: ../result/enum.Result.html#variant.Ok
///
/// # Examples
///
@@ -89,17 +99,32 @@ pub fn empty() -> Empty { Empty { _priv: () } }
#[stable(feature = "rust1", since = "1.0.0")]
impl Read for Empty {
+ #[inline]
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { Ok(0) }
+
+ #[inline]
+ unsafe fn initializer(&self) -> Initializer {
+ Initializer::nop()
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl BufRead for Empty {
+ #[inline]
fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) }
+ #[inline]
fn consume(&mut self, _n: usize) {}
}
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Empty {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.pad("Empty { .. }")
+ }
+}
+
/// A reader which yields one byte over and over and over and over and over and...
///
-/// This struct is generally created by calling [`repeat()`][repeat]. Please
+/// This struct is generally created by calling [`repeat`][repeat]. Please
/// see the documentation of `repeat()` for more details.
///
/// [repeat]: fn.repeat.html
@@ -125,17 +150,30 @@ pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } }
#[stable(feature = "rust1", since = "1.0.0")]
impl Read for Repeat {
+ #[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
for slot in &mut *buf {
*slot = self.byte;
}
Ok(buf.len())
}
+
+ #[inline]
+ unsafe fn initializer(&self) -> Initializer {
+ Initializer::nop()
+ }
+}
+
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Repeat {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.pad("Repeat { .. }")
+ }
}
/// A writer which will move data into the void.
///
-/// This struct is generally created by calling [`sink()`][sink]. Please
+/// This struct is generally created by calling [`sink`][sink]. Please
/// see the documentation of `sink()` for more details.
///
/// [sink]: fn.sink.html
@@ -161,10 +199,19 @@ pub fn sink() -> Sink { Sink { _priv: () } }
#[stable(feature = "rust1", since = "1.0.0")]
impl Write for Sink {
+ #[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
+ #[inline]
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
+#[stable(feature = "std_debug", since = "1.16.0")]
+impl fmt::Debug for Sink {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.pad("Sink { .. }")
+ }
+}
+
#[cfg(test)]
mod tests {
use io::prelude::*;