2015-02-01 04:24:36 +00:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
#![allow(missing_copy_implementations)]
|
|
|
|
|
2016-11-25 18:21:49 +00:00
|
|
|
use fmt;
|
2017-05-15 01:29:18 +00:00
|
|
|
use io::{self, Read, Initializer, Write, ErrorKind, BufRead};
|
|
|
|
use mem;
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
/// Copies the entire contents of a reader into a writer.
|
|
|
|
///
|
2015-05-20 20:02:46 +00:00
|
|
|
/// This function will continuously read data from `reader` and then
|
|
|
|
/// write it into `writer` in a streaming fashion until `reader`
|
|
|
|
/// returns EOF.
|
2015-02-01 04:24:36 +00:00
|
|
|
///
|
2015-05-20 20:02:46 +00:00
|
|
|
/// On success, the total number of bytes that were copied from
|
|
|
|
/// `reader` to `writer` is returned.
|
2015-02-01 04:24:36 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// This function will return an error immediately if any call to `read` or
|
|
|
|
/// `write` returns an error. All instances of `ErrorKind::Interrupted` are
|
|
|
|
/// handled by this function and the underlying operation is retried.
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// let mut reader: &[u8] = b"hello";
|
|
|
|
/// let mut writer: Vec<u8> = vec![];
|
|
|
|
///
|
2016-12-28 09:02:35 +00:00
|
|
|
/// io::copy(&mut reader, &mut writer)?;
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(reader, &writer[..]);
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-08-05 11:31:05 +00:00
|
|
|
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
|
|
|
|
where R: Read, W: Write
|
|
|
|
{
|
2017-05-15 01:29:18 +00:00
|
|
|
let mut buf = unsafe {
|
|
|
|
let mut buf: [u8; super::DEFAULT_BUF_SIZE] = mem::uninitialized();
|
|
|
|
reader.initializer().initialize(&mut buf);
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
let mut written = 0;
|
|
|
|
loop {
|
2015-05-20 20:02:46 +00:00
|
|
|
let len = match reader.read(&mut buf) {
|
2015-02-01 04:24:36 +00:00
|
|
|
Ok(0) => return Ok(written),
|
|
|
|
Ok(len) => len,
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2016-03-23 03:01:37 +00:00
|
|
|
writer.write_all(&buf[..len])?;
|
2015-02-01 04:24:36 +00:00
|
|
|
written += len as u64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A reader which is always at EOF.
|
2015-07-20 18:41:00 +00:00
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// This struct is generally created by calling [`empty`][empty]. Please see
|
2015-07-20 18:41:00 +00:00
|
|
|
/// the documentation of `empty()` for more details.
|
|
|
|
///
|
|
|
|
/// [empty]: fn.empty.html
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub struct Empty { _priv: () }
|
|
|
|
|
2015-07-08 22:31:08 +00:00
|
|
|
/// Constructs a new handle to an empty reader.
|
2015-02-01 04:24:36 +00:00
|
|
|
///
|
|
|
|
/// All reads from the returned reader will return `Ok(0)`.
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// A slightly sad example of not reading anything into a buffer:
|
|
|
|
///
|
|
|
|
/// ```
|
2016-06-28 01:13:26 +00:00
|
|
|
/// use std::io::{self, Read};
|
2015-07-08 22:31:08 +00:00
|
|
|
///
|
|
|
|
/// let mut buffer = String::new();
|
2016-06-28 01:13:26 +00:00
|
|
|
/// io::empty().read_to_string(&mut buffer).unwrap();
|
|
|
|
/// assert!(buffer.is_empty());
|
2015-07-08 22:31:08 +00:00
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub fn empty() -> Empty { Empty { _priv: () } }
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
impl Read for Empty {
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
2015-02-01 04:24:36 +00:00
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { Ok(0) }
|
2017-05-15 01:29:18 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::nop()
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl BufRead for Empty {
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) }
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
fn consume(&mut self, _n: usize) {}
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
impl fmt::Debug for Empty {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("Empty { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:41:00 +00:00
|
|
|
/// A reader which yields one byte over and over and over and over and over and...
|
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// This struct is generally created by calling [`repeat`][repeat]. Please
|
2015-07-20 18:41:00 +00:00
|
|
|
/// see the documentation of `repeat()` for more details.
|
|
|
|
///
|
2015-07-28 02:25:06 +00:00
|
|
|
/// [repeat]: fn.repeat.html
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub struct Repeat { byte: u8 }
|
|
|
|
|
|
|
|
/// Creates an instance of a reader that infinitely repeats one byte.
|
|
|
|
///
|
|
|
|
/// All reads from this reader will succeed by filling the specified buffer with
|
|
|
|
/// the given byte.
|
2016-06-28 01:23:53 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
///
|
|
|
|
/// let mut buffer = [0; 3];
|
|
|
|
/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
|
|
|
|
/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub fn repeat(byte: u8) -> Repeat { Repeat { byte: byte } }
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
impl Read for Repeat {
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
2015-02-01 04:24:36 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2015-06-10 16:22:20 +00:00
|
|
|
for slot in &mut *buf {
|
2015-02-01 04:24:36 +00:00
|
|
|
*slot = self.byte;
|
|
|
|
}
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::nop()
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
impl fmt::Debug for Repeat {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("Repeat { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
/// A writer which will move data into the void.
|
2015-07-20 18:41:00 +00:00
|
|
|
///
|
2017-03-12 18:04:52 +00:00
|
|
|
/// This struct is generally created by calling [`sink`][sink]. Please
|
2015-07-20 18:41:00 +00:00
|
|
|
/// see the documentation of `sink()` for more details.
|
|
|
|
///
|
2015-07-28 02:25:06 +00:00
|
|
|
/// [sink]: fn.sink.html
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub struct Sink { _priv: () }
|
|
|
|
|
|
|
|
/// Creates an instance of a writer which will successfully consume all data.
|
|
|
|
///
|
|
|
|
/// All calls to `write` on the returned instance will return `Ok(buf.len())`
|
|
|
|
/// and the contents of the buffer will not be inspected.
|
2016-06-28 12:22:34 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
///
|
2016-07-08 01:37:31 +00:00
|
|
|
/// let buffer = vec![1, 2, 3, 5, 8];
|
|
|
|
/// let num_bytes = io::sink().write(&buffer).unwrap();
|
2016-06-28 12:22:34 +00:00
|
|
|
/// assert_eq!(num_bytes, 5);
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub fn sink() -> Sink { Sink { _priv: () } }
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
impl Write for Sink {
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
2015-02-01 04:24:36 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) }
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
2015-02-01 04:24:36 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 18:21:49 +00:00
|
|
|
impl fmt::Debug for Sink {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad("Sink { .. }")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
#[cfg(test)]
|
2015-04-24 15:30:41 +00:00
|
|
|
mod tests {
|
2015-02-01 04:24:36 +00:00
|
|
|
use io::prelude::*;
|
2015-08-05 11:31:05 +00:00
|
|
|
use io::{copy, sink, empty, repeat};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_copies() {
|
|
|
|
let mut r = repeat(0).take(4);
|
|
|
|
let mut w = sink();
|
|
|
|
assert_eq!(copy(&mut r, &mut w).unwrap(), 4);
|
|
|
|
|
|
|
|
let mut r = repeat(0).take(1 << 17);
|
|
|
|
assert_eq!(copy(&mut r as &mut Read, &mut w as &mut Write).unwrap(), 1 << 17);
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sink_sinks() {
|
|
|
|
let mut s = sink();
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(s.write(&[]).unwrap(), 0);
|
|
|
|
assert_eq!(s.write(&[0]).unwrap(), 1);
|
|
|
|
assert_eq!(s.write(&[0; 1024]).unwrap(), 1024);
|
|
|
|
assert_eq!(s.by_ref().write(&[0; 1024]).unwrap(), 1024);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_reads() {
|
|
|
|
let mut e = empty();
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(e.read(&mut []).unwrap(), 0);
|
|
|
|
assert_eq!(e.read(&mut [0]).unwrap(), 0);
|
|
|
|
assert_eq!(e.read(&mut [0; 1024]).unwrap(), 0);
|
|
|
|
assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn repeat_repeats() {
|
|
|
|
let mut r = repeat(4);
|
|
|
|
let mut b = [0; 1024];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(r.read(&mut b).unwrap(), 1024);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert!(b.iter().all(|b| *b == 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_some_bytes() {
|
|
|
|
assert_eq!(repeat(4).take(100).bytes().count(), 100);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);
|
|
|
|
}
|
|
|
|
}
|