|
@@ -11,9 +11,10 @@
|
|
#![allow(missing_copy_implementations)]
|
|
#![allow(missing_copy_implementations)]
|
|
|
|
|
|
use core::fmt;
|
|
use core::fmt;
|
|
-use io::{self, Read, Initializer, Write, ErrorKind};
|
|
|
|
-use core::mem;
|
|
|
|
-#[cfg(feature="alloc")] use io::BufRead;
|
|
|
|
|
|
+use io::{self, ErrorKind, Initializer, Read, Write};
|
|
|
|
+
|
|
|
|
+#[cfg(feature = "alloc")]
|
|
|
|
+use io::BufRead;
|
|
|
|
|
|
/// Copies the entire contents of a reader into a writer.
|
|
/// Copies the entire contents of a reader into a writer.
|
|
///
|
|
///
|
|
@@ -51,10 +52,12 @@ use core::mem;
|
|
/// }
|
|
/// }
|
|
/// ```
|
|
/// ```
|
|
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
|
|
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
|
|
- where R: Read, W: Write
|
|
|
|
|
|
+where
|
|
|
|
+ R: Read,
|
|
|
|
+ W: Write,
|
|
{
|
|
{
|
|
let mut buf = unsafe {
|
|
let mut buf = unsafe {
|
|
- let mut buf: [u8; super::DEFAULT_BUF_SIZE] = mem::uninitialized();
|
|
|
|
|
|
+ let mut buf: [u8; super::DEFAULT_BUF_SIZE] = [0u8; super::DEFAULT_BUF_SIZE];
|
|
reader.initializer().initialize(&mut buf);
|
|
reader.initializer().initialize(&mut buf);
|
|
buf
|
|
buf
|
|
};
|
|
};
|
|
@@ -78,7 +81,9 @@ pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<
|
|
/// the documentation of [`empty()`][`empty`] for more details.
|
|
/// the documentation of [`empty()`][`empty`] for more details.
|
|
///
|
|
///
|
|
/// [`empty`]: fn.empty.html
|
|
/// [`empty`]: fn.empty.html
|
|
-pub struct Empty { _priv: () }
|
|
|
|
|
|
+pub struct Empty {
|
|
|
|
+ _priv: (),
|
|
|
|
+}
|
|
|
|
|
|
/// Constructs a new handle to an empty reader.
|
|
/// Constructs a new handle to an empty reader.
|
|
///
|
|
///
|
|
@@ -97,11 +102,15 @@ pub struct Empty { _priv: () }
|
|
/// io::empty().read_to_string(&mut buffer).unwrap();
|
|
/// io::empty().read_to_string(&mut buffer).unwrap();
|
|
/// assert!(buffer.is_empty());
|
|
/// assert!(buffer.is_empty());
|
|
/// ```
|
|
/// ```
|
|
-pub fn empty() -> Empty { Empty { _priv: () } }
|
|
|
|
|
|
+pub fn empty() -> Empty {
|
|
|
|
+ Empty { _priv: () }
|
|
|
|
+}
|
|
|
|
|
|
impl Read for Empty {
|
|
impl Read for Empty {
|
|
#[inline]
|
|
#[inline]
|
|
- fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { Ok(0) }
|
|
|
|
|
|
+ fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
+ Ok(0)
|
|
|
|
+ }
|
|
|
|
|
|
#[inline]
|
|
#[inline]
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
@@ -109,10 +118,12 @@ impl Read for Empty {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-#[cfg(feature="alloc")]
|
|
|
|
|
|
+#[cfg(feature = "alloc")]
|
|
impl BufRead for Empty {
|
|
impl BufRead for Empty {
|
|
#[inline]
|
|
#[inline]
|
|
- fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) }
|
|
|
|
|
|
+ fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
|
|
|
+ Ok(&[])
|
|
|
|
+ }
|
|
#[inline]
|
|
#[inline]
|
|
fn consume(&mut self, _n: usize) {}
|
|
fn consume(&mut self, _n: usize) {}
|
|
}
|
|
}
|
|
@@ -129,7 +140,9 @@ impl fmt::Debug for Empty {
|
|
/// see the documentation of `repeat()` for more details.
|
|
/// see the documentation of `repeat()` for more details.
|
|
///
|
|
///
|
|
/// [repeat]: fn.repeat.html
|
|
/// [repeat]: fn.repeat.html
|
|
-pub struct Repeat { byte: u8 }
|
|
|
|
|
|
+pub struct Repeat {
|
|
|
|
+ byte: u8,
|
|
|
|
+}
|
|
|
|
|
|
/// Creates an instance of a reader that infinitely repeats one byte.
|
|
/// Creates an instance of a reader that infinitely repeats one byte.
|
|
///
|
|
///
|
|
@@ -145,7 +158,9 @@ pub struct Repeat { byte: u8 }
|
|
/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
|
|
/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
|
|
/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
|
|
/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
|
|
/// ```
|
|
/// ```
|
|
-pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } }
|
|
|
|
|
|
+pub fn repeat(byte: u8) -> Repeat {
|
|
|
|
+ Repeat { byte: byte }
|
|
|
|
+}
|
|
|
|
|
|
impl Read for Repeat {
|
|
impl Read for Repeat {
|
|
#[inline]
|
|
#[inline]
|
|
@@ -174,7 +189,9 @@ impl fmt::Debug for Repeat {
|
|
/// see the documentation of `sink()` for more details.
|
|
/// see the documentation of `sink()` for more details.
|
|
///
|
|
///
|
|
/// [sink]: fn.sink.html
|
|
/// [sink]: fn.sink.html
|
|
-pub struct Sink { _priv: () }
|
|
|
|
|
|
+pub struct Sink {
|
|
|
|
+ _priv: (),
|
|
|
|
+}
|
|
|
|
|
|
/// Creates an instance of a writer which will successfully consume all data.
|
|
/// Creates an instance of a writer which will successfully consume all data.
|
|
///
|
|
///
|
|
@@ -190,13 +207,19 @@ pub struct Sink { _priv: () }
|
|
/// let num_bytes = io::sink().write(&buffer).unwrap();
|
|
/// let num_bytes = io::sink().write(&buffer).unwrap();
|
|
/// assert_eq!(num_bytes, 5);
|
|
/// assert_eq!(num_bytes, 5);
|
|
/// ```
|
|
/// ```
|
|
-pub fn sink() -> Sink { Sink { _priv: () } }
|
|
|
|
|
|
+pub fn sink() -> Sink {
|
|
|
|
+ Sink { _priv: () }
|
|
|
|
+}
|
|
|
|
|
|
impl Write for Sink {
|
|
impl Write for Sink {
|
|
#[inline]
|
|
#[inline]
|
|
- fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
|
|
|
|
|
|
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
+ Ok(buf.len())
|
|
|
|
+ }
|
|
#[inline]
|
|
#[inline]
|
|
- fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
|
|
|
|
|
+ fn flush(&mut self) -> io::Result<()> {
|
|
|
|
+ Ok(())
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl fmt::Debug for Sink {
|
|
impl fmt::Debug for Sink {
|
|
@@ -208,7 +231,7 @@ impl fmt::Debug for Sink {
|
|
#[cfg(test)]
|
|
#[cfg(test)]
|
|
mod tests {
|
|
mod tests {
|
|
use io::prelude::*;
|
|
use io::prelude::*;
|
|
- use io::{copy, sink, empty, repeat};
|
|
|
|
|
|
+ use io::{copy, empty, repeat, sink};
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
fn copy_copies() {
|
|
fn copy_copies() {
|
|
@@ -217,7 +240,10 @@ mod tests {
|
|
assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
|
|
assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
|
|
|
|
|
|
let mut r = repeat(0).take(1 << 17);
|
|
let mut r = repeat(0).take(1 << 17);
|
|
- assert_eq!(copy(&mut r as &mut dyn Read, &mut w as &mut dyn Write).unwrap(), 1 << 17);
|
|
|
|
|
|
+ assert_eq!(
|
|
|
|
+ copy(&mut r as &mut dyn Read, &mut w as &mut dyn Write).unwrap(),
|
|
|
|
+ 1 << 17
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
@@ -250,6 +276,9 @@ mod tests {
|
|
fn take_some_bytes() {
|
|
fn take_some_bytes() {
|
|
assert_eq!(repeat(4).take(100).bytes().count(), 100);
|
|
assert_eq!(repeat(4).take(100).bytes().count(), 100);
|
|
assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
|
|
assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
|
|
- assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
|
|
|
|
|
|
+ assert_eq!(
|
|
|
|
+ repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(),
|
|
|
|
+ 20
|
|
|
|
+ );
|
|
}
|
|
}
|
|
}
|
|
}
|