1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(missing_copy_implementations)]
13 use io
::{self, Read, Write, ErrorKind, BufRead}
;
15 /// Copies the entire contents of a reader into a writer.
17 /// This function will continuously read data from `reader` and then
18 /// write it into `writer` in a streaming fashion until `reader`
21 /// On success, the total number of bytes that were copied from
22 /// `reader` to `writer` is returned.
26 /// This function will return an error immediately if any call to `read` or
27 /// `write` returns an error. All instances of `ErrorKind::Interrupted` are
28 /// handled by this function and the underlying operation is retried.
35 /// # fn foo() -> io::Result<()> {
36 /// let mut reader: &[u8] = b"hello";
37 /// let mut writer: Vec<u8> = vec![];
39 /// try!(io::copy(&mut reader, &mut writer));
41 /// assert_eq!(reader, &writer[..]);
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub fn copy
<R
: ?Sized
, W
: ?Sized
>(reader
: &mut R
, writer
: &mut W
) -> io
::Result
<u64>
47 where R
: Read
, W
: Write
49 let mut buf
= [0; super::DEFAULT_BUF_SIZE
];
52 let len
= match reader
.read(&mut buf
) {
53 Ok(0) => return Ok(written
),
55 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> continue,
56 Err(e
) => return Err(e
),
58 writer
.write_all(&buf
[..len
])?
;
59 written
+= len
as u64;
63 /// A reader which is always at EOF.
65 /// This struct is generally created by calling [`empty()`][empty]. Please see
66 /// the documentation of `empty()` for more details.
68 /// [empty]: fn.empty.html
69 #[stable(feature = "rust1", since = "1.0.0")]
70 pub struct Empty { _priv: () }
72 /// Constructs a new handle to an empty reader.
74 /// All reads from the returned reader will return `Ok(0)`.
78 /// A slightly sad example of not reading anything into a buffer:
82 /// use std::io::Read;
84 /// # fn foo() -> io::Result<String> {
85 /// let mut buffer = String::new();
86 /// try!(io::empty().read_to_string(&mut buffer));
90 #[stable(feature = "rust1", since = "1.0.0")]
91 pub fn empty() -> Empty { Empty { _priv: () }
}
93 #[stable(feature = "rust1", since = "1.0.0")]
95 fn read(&mut self, _buf
: &mut [u8]) -> io
::Result
<usize> { Ok(0) }
97 #[stable(feature = "rust1", since = "1.0.0")]
98 impl BufRead
for Empty
{
99 fn fill_buf(&mut self) -> io
::Result
<&[u8]> { Ok(&[]) }
100 fn consume(&mut self, _n
: usize) {}
103 /// A reader which yields one byte over and over and over and over and over and...
105 /// This struct is generally created by calling [`repeat()`][repeat]. Please
106 /// see the documentation of `repeat()` for more details.
108 /// [repeat]: fn.repeat.html
109 #[stable(feature = "rust1", since = "1.0.0")]
110 pub struct Repeat { byte: u8 }
112 /// Creates an instance of a reader that infinitely repeats one byte.
114 /// All reads from this reader will succeed by filling the specified buffer with
116 #[stable(feature = "rust1", since = "1.0.0")]
117 pub fn repeat(byte
: u8) -> Repeat { Repeat { byte: byte }
}
119 #[stable(feature = "rust1", since = "1.0.0")]
120 impl Read
for Repeat
{
121 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
122 for slot
in &mut *buf
{
129 /// A writer which will move data into the void.
131 /// This struct is generally created by calling [`sink()`][sink]. Please
132 /// see the documentation of `sink()` for more details.
134 /// [sink]: fn.sink.html
135 #[stable(feature = "rust1", since = "1.0.0")]
136 pub struct Sink { _priv: () }
138 /// Creates an instance of a writer which will successfully consume all data.
140 /// All calls to `write` on the returned instance will return `Ok(buf.len())`
141 /// and the contents of the buffer will not be inspected.
142 #[stable(feature = "rust1", since = "1.0.0")]
143 pub fn sink() -> Sink { Sink { _priv: () }
}
145 #[stable(feature = "rust1", since = "1.0.0")]
146 impl Write
for Sink
{
147 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> { Ok(buf.len()) }
148 fn flush(&mut self) -> io
::Result
<()> { Ok(()) }
156 use io
::{copy, sink, empty, repeat}
;
160 let mut r
= repeat(0).take(4);
162 assert_eq
!(copy(&mut r
, &mut w
).unwrap(), 4);
164 let mut r
= repeat(0).take(1 << 17);
165 assert_eq
!(copy(&mut r
as &mut Read
, &mut w
as &mut Write
).unwrap(), 1 << 17);
171 assert_eq
!(s
.write(&[]).unwrap(), 0);
172 assert_eq
!(s
.write(&[0]).unwrap(), 1);
173 assert_eq
!(s
.write(&[0; 1024]).unwrap(), 1024);
174 assert_eq
!(s
.by_ref().write(&[0; 1024]).unwrap(), 1024);
180 assert_eq
!(e
.read(&mut []).unwrap(), 0);
181 assert_eq
!(e
.read(&mut [0]).unwrap(), 0);
182 assert_eq
!(e
.read(&mut [0; 1024]).unwrap(), 0);
183 assert_eq
!(e
.by_ref().read(&mut [0; 1024]).unwrap(), 0);
187 fn repeat_repeats() {
188 let mut r
= repeat(4);
189 let mut b
= [0; 1024];
190 assert_eq
!(r
.read(&mut b
).unwrap(), 1024);
191 assert
!(b
.iter().all(|b
| *b
== 4));
195 fn take_some_bytes() {
196 assert_eq
!(repeat(4).take(100).bytes().count(), 100);
197 assert_eq
!(repeat(4).take(100).bytes().next().unwrap().unwrap(), 4);
198 assert_eq
!(repeat(1).take(10).chain(repeat(2).take(10)).bytes().count(), 20);