]>
git.proxmox.com Git - rustc.git/blob - library/std/src/io/util.rs
1 #![allow(missing_copy_implementations)]
8 self, BufRead
, Initializer
, IoSlice
, IoSliceMut
, Read
, Seek
, SeekFrom
, SizeHint
, Write
,
11 /// A reader which is always at EOF.
13 /// This struct is generally created by calling [`empty()`]. Please see
14 /// the documentation of [`empty()`] for more details.
15 #[stable(feature = "rust1", since = "1.0.0")]
17 #[derive(Copy, Clone, Default)]
20 /// Constructs a new handle to an empty reader.
22 /// All reads from the returned reader will return <code>[Ok]\(0)</code>.
26 /// A slightly sad example of not reading anything into a buffer:
29 /// use std::io::{self, Read};
31 /// let mut buffer = String::new();
32 /// io::empty().read_to_string(&mut buffer).unwrap();
33 /// assert!(buffer.is_empty());
36 #[stable(feature = "rust1", since = "1.0.0")]
37 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
38 pub const fn empty() -> Empty
{
42 #[stable(feature = "rust1", since = "1.0.0")]
45 fn read(&mut self, _buf
: &mut [u8]) -> io
::Result
<usize> {
50 unsafe fn initializer(&self) -> Initializer
{
54 #[stable(feature = "rust1", since = "1.0.0")]
55 impl BufRead
for Empty
{
57 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
61 fn consume(&mut self, _n
: usize) {}
64 #[stable(feature = "empty_seek", since = "1.51.0")]
66 fn seek(&mut self, _pos
: SeekFrom
) -> io
::Result
<u64> {
70 fn stream_len(&mut self) -> io
::Result
<u64> {
74 fn stream_position(&mut self) -> io
::Result
<u64> {
79 #[stable(feature = "std_debug", since = "1.16.0")]
80 impl fmt
::Debug
for Empty
{
81 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
82 f
.debug_struct("Empty").finish_non_exhaustive()
86 impl SizeHint
for Empty
{
88 fn upper_bound(&self) -> Option
<usize> {
93 /// A reader which yields one byte over and over and over and over and over and...
95 /// This struct is generally created by calling [`repeat()`]. Please
96 /// see the documentation of [`repeat()`] for more details.
97 #[stable(feature = "rust1", since = "1.0.0")]
102 /// Creates an instance of a reader that infinitely repeats one byte.
104 /// All reads from this reader will succeed by filling the specified buffer with
110 /// use std::io::{self, Read};
112 /// let mut buffer = [0; 3];
113 /// io::repeat(0b101).read_exact(&mut buffer).unwrap();
114 /// assert_eq!(buffer, [0b101, 0b101, 0b101]);
117 #[stable(feature = "rust1", since = "1.0.0")]
118 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
119 pub const fn repeat(byte
: u8) -> Repeat
{
123 #[stable(feature = "rust1", since = "1.0.0")]
124 impl Read
for Repeat
{
126 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
127 for slot
in &mut *buf
{
134 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
135 let mut nwritten
= 0;
137 nwritten
+= self.read(buf
)?
;
143 fn is_read_vectored(&self) -> bool
{
148 unsafe fn initializer(&self) -> Initializer
{
153 impl SizeHint
for Repeat
{
155 fn lower_bound(&self) -> usize {
160 fn upper_bound(&self) -> Option
<usize> {
165 #[stable(feature = "std_debug", since = "1.16.0")]
166 impl fmt
::Debug
for Repeat
{
167 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
168 f
.debug_struct("Repeat").finish_non_exhaustive()
172 /// A writer which will move data into the void.
174 /// This struct is generally created by calling [`sink`]. Please
175 /// see the documentation of [`sink()`] for more details.
176 #[stable(feature = "rust1", since = "1.0.0")]
178 #[derive(Copy, Clone, Default)]
181 /// Creates an instance of a writer which will successfully consume all data.
183 /// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
184 /// and the contents of the buffer will not be inspected.
186 /// [`write`]: Write::write
191 /// use std::io::{self, Write};
193 /// let buffer = vec![1, 2, 3, 5, 8];
194 /// let num_bytes = io::sink().write(&buffer).unwrap();
195 /// assert_eq!(num_bytes, 5);
198 #[stable(feature = "rust1", since = "1.0.0")]
199 #[rustc_const_unstable(feature = "const_io_structs", issue = "78812")]
200 pub const fn sink() -> Sink
{
204 #[stable(feature = "rust1", since = "1.0.0")]
205 impl Write
for Sink
{
207 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
212 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
213 let total_len
= bufs
.iter().map(|b
| b
.len()).sum();
218 fn is_write_vectored(&self) -> bool
{
223 fn flush(&mut self) -> io
::Result
<()> {
228 #[stable(feature = "write_mt", since = "1.48.0")]
229 impl Write
for &Sink
{
231 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
236 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
237 let total_len
= bufs
.iter().map(|b
| b
.len()).sum();
242 fn is_write_vectored(&self) -> bool
{
247 fn flush(&mut self) -> io
::Result
<()> {
252 #[stable(feature = "std_debug", since = "1.16.0")]
253 impl fmt
::Debug
for Sink
{
254 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
255 f
.debug_struct("Sink").finish_non_exhaustive()