1use crate::alloc::Allocator;
2use crate::boxed::Box;
3#[cfg(not(no_global_oom_handling))]
4use crate::collections::VecDeque;
5use crate::fmt;
6use crate::io::{self, IoSlice, Seek, SeekFrom, SizeHint, Write};
7#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
8use crate::sync::Arc;
9use crate::vec::Vec;
10
11#[doc(hidden)]
15#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
16impl<T> SizeHint for Box<T> {
17 #[inline]
18 fn lower_bound(&self) -> usize {
19 SizeHint::lower_bound(&**self)
20 }
21
22 #[inline]
23 fn upper_bound(&self) -> Option<usize> {
24 SizeHint::upper_bound(&**self)
25 }
26}
27
28#[stable(feature = "rust1", since = "1.0.0")]
29impl<W: Write + ?Sized> Write for Box<W> {
30 #[inline]
31 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
32 (**self).write(buf)
33 }
34
35 #[inline]
36 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
37 (**self).write_vectored(bufs)
38 }
39
40 #[inline]
41 fn is_write_vectored(&self) -> bool {
42 (**self).is_write_vectored()
43 }
44
45 #[inline]
46 fn flush(&mut self) -> io::Result<()> {
47 (**self).flush()
48 }
49
50 #[inline]
51 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
52 (**self).write_all(buf)
53 }
54
55 #[inline]
56 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
57 (**self).write_all_vectored(bufs)
58 }
59
60 #[inline]
61 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
62 (**self).write_fmt(fmt)
63 }
64}
65#[stable(feature = "rust1", since = "1.0.0")]
66impl<S: Seek + ?Sized> Seek for Box<S> {
67 #[inline]
68 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
69 (**self).seek(pos)
70 }
71
72 #[inline]
73 fn rewind(&mut self) -> io::Result<()> {
74 (**self).rewind()
75 }
76
77 #[inline]
78 fn stream_len(&mut self) -> io::Result<u64> {
79 (**self).stream_len()
80 }
81
82 #[inline]
83 fn stream_position(&mut self) -> io::Result<u64> {
84 (**self).stream_position()
85 }
86
87 #[inline]
88 fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
89 (**self).seek_relative(offset)
90 }
91}
92
93#[stable(feature = "rust1", since = "1.0.0")]
99impl<A: Allocator> Write for Vec<u8, A> {
100 #[inline]
101 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
102 <Self as Write>::write_all(self, buf)?;
103 Ok(buf.len())
104 }
105
106 #[inline]
107 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
108 let len = bufs.iter().map(|b| b.len()).sum();
109 cfg_select! {
110 no_global_oom_handling => {
111 self.try_reserve(len)?;
112 }
113 _ => {
114 self.reserve(len);
115 }
116 }
117 for buf in bufs {
118 <Self as Write>::write_all(self, buf)?;
119 }
120 Ok(len)
121 }
122
123 #[inline]
124 fn is_write_vectored(&self) -> bool {
125 true
126 }
127
128 #[inline]
129 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
130 cfg_select! {
131 no_global_oom_handling => {
132 self.try_extend_from_slice_of_bytes(buf)?;
133 }
134 _ => {
135 self.extend_from_slice(buf);
136 }
137 }
138 Ok(())
139 }
140
141 #[inline]
142 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
143 self.write_vectored(bufs)?;
144 Ok(())
145 }
146
147 #[inline]
148 fn flush(&mut self) -> io::Result<()> {
149 Ok(())
150 }
151}
152
153#[cfg(not(no_global_oom_handling))]
155#[stable(feature = "vecdeque_read_write", since = "1.63.0")]
156impl<A: Allocator> Write for VecDeque<u8, A> {
157 #[inline]
158 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
159 self.extend(buf);
160 Ok(buf.len())
161 }
162
163 #[inline]
164 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
165 let len = bufs.iter().map(|b| b.len()).sum();
166 self.reserve(len);
167 for buf in bufs {
168 self.extend(&**buf);
169 }
170 Ok(len)
171 }
172
173 #[inline]
174 fn is_write_vectored(&self) -> bool {
175 true
176 }
177
178 #[inline]
179 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
180 self.extend(buf);
181 Ok(())
182 }
183
184 #[inline]
185 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
186 self.write_vectored(bufs)?;
187 Ok(())
188 }
189
190 #[inline]
191 fn flush(&mut self) -> io::Result<()> {
192 Ok(())
193 }
194}
195
196#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
197#[stable(feature = "io_traits_arc", since = "1.73.0")]
198impl<W: Write + ?Sized> Write for Arc<W>
199where
200 for<'a> &'a W: Write,
201 W: crate::io::IoHandle,
202{
203 #[inline]
204 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
205 (&**self).write(buf)
206 }
207
208 #[inline]
209 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
210 (&**self).write_vectored(bufs)
211 }
212
213 #[inline]
214 fn is_write_vectored(&self) -> bool {
215 (&**self).is_write_vectored()
216 }
217
218 #[inline]
219 fn flush(&mut self) -> io::Result<()> {
220 (&**self).flush()
221 }
222
223 #[inline]
224 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
225 (&**self).write_all(buf)
226 }
227
228 #[inline]
229 fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
230 (&**self).write_all_vectored(bufs)
231 }
232
233 #[inline]
234 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
235 (&**self).write_fmt(fmt)
236 }
237}
238#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
239#[stable(feature = "io_traits_arc", since = "1.73.0")]
240impl<S: Seek + ?Sized> Seek for Arc<S>
241where
242 for<'a> &'a S: Seek,
243 S: crate::io::IoHandle,
244{
245 #[inline]
246 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
247 (&**self).seek(pos)
248 }
249
250 #[inline]
251 fn rewind(&mut self) -> io::Result<()> {
252 (&**self).rewind()
253 }
254
255 #[inline]
256 fn stream_len(&mut self) -> io::Result<u64> {
257 (&**self).stream_len()
258 }
259
260 #[inline]
261 fn stream_position(&mut self) -> io::Result<u64> {
262 (&**self).stream_position()
263 }
264
265 #[inline]
266 fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
267 (&**self).seek_relative(offset)
268 }
269}