1 use crate::io
::util
::flush
::{flush, Flush}
;
2 use crate::io
::util
::shutdown
::{shutdown, Shutdown}
;
3 use crate::io
::util
::write
::{write, Write}
;
4 use crate::io
::util
::write_all
::{write_all, WriteAll}
;
5 use crate::io
::util
::write_all_buf
::{write_all_buf, WriteAllBuf}
;
6 use crate::io
::util
::write_buf
::{write_buf, WriteBuf}
;
7 use crate::io
::util
::write_int
::{
8 WriteI128
, WriteI128Le
, WriteI16
, WriteI16Le
, WriteI32
, WriteI32Le
, WriteI64
, WriteI64Le
,
11 use crate::io
::util
::write_int
::{
12 WriteU128
, WriteU128Le
, WriteU16
, WriteU16Le
, WriteU32
, WriteU32Le
, WriteU64
, WriteU64Le
,
15 use crate::io
::util
::write_vectored
::{write_vectored, WriteVectored}
;
16 use crate::io
::AsyncWrite
;
22 /// Defines numeric writer
23 macro_rules
! write_impl
{
27 fn $name
:ident(&mut self, n
: $ty
:ty
) -> $
($fut
:ident
)*;
32 fn $name
<'a
>(&'a
mut self, n
: $ty
) -> $
($fut
)*<&'a
mut Self> where Self: Unpin
{
33 $
($fut
)*::new(self, n
)
39 /// Writes bytes to a sink.
41 /// Implemented as an extension trait, adding utility methods to all
42 /// [`AsyncWrite`] types. Callers will tend to import this trait instead of
46 /// use tokio::io::{self, AsyncWriteExt};
47 /// use tokio::fs::File;
50 /// async fn main() -> io::Result<()> {
51 /// let data = b"some bytes";
54 /// let mut buffer = File::create("foo.txt").await?;
56 /// while pos < data.len() {
57 /// let bytes_written = buffer.write(&data[pos..]).await?;
58 /// pos += bytes_written;
65 /// See [module][crate::io] documentation for more details.
67 /// [`AsyncWrite`]: AsyncWrite
68 pub trait AsyncWriteExt
: AsyncWrite
{
69 /// Writes a buffer into this writer, returning how many bytes were
75 /// async fn write(&mut self, buf: &[u8]) -> io::Result<usize>;
78 /// This function will attempt to write the entire contents of `buf`, but
79 /// the entire write may not succeed, or the write may also generate an
80 /// error. A call to `write` represents *at most one* attempt to write to
81 /// any wrapped object.
85 /// If the return value is `Ok(n)` then it must be guaranteed that `n <=
86 /// buf.len()`. A return value of `0` typically means that the
87 /// underlying object is no longer able to accept bytes and will likely
88 /// not be able to in the future as well, or that the buffer provided is
93 /// Each call to `write` may generate an I/O error indicating that the
94 /// operation could not be completed. If an error is returned then no bytes
95 /// in the buffer were written to this writer.
97 /// It is **not** considered an error if the entire buffer could not be
98 /// written to this writer.
102 /// This method is cancellation safe in the sense that if it is used as
103 /// the event in a [`tokio::select!`](crate::select) statement and some
104 /// other branch completes first, then it is guaranteed that no data was
105 /// written to this `AsyncWrite`.
110 /// use tokio::io::{self, AsyncWriteExt};
111 /// use tokio::fs::File;
114 /// async fn main() -> io::Result<()> {
115 /// let mut file = File::create("foo.txt").await?;
117 /// // Writes some prefix of the byte string, not necessarily all of it.
118 /// file.write(b"some bytes").await?;
122 fn write
<'a
>(&'a
mut self, src
: &'a
[u8]) -> Write
<'a
, Self>
129 /// Like [`write`], except that it writes from a slice of buffers.
134 /// async fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize>;
137 /// See [`AsyncWrite::poll_write_vectored`] for more details.
141 /// This method is cancellation safe in the sense that if it is used as
142 /// the event in a [`tokio::select!`](crate::select) statement and some
143 /// other branch completes first, then it is guaranteed that no data was
144 /// written to this `AsyncWrite`.
149 /// use tokio::io::{self, AsyncWriteExt};
150 /// use tokio::fs::File;
151 /// use std::io::IoSlice;
154 /// async fn main() -> io::Result<()> {
155 /// let mut file = File::create("foo.txt").await?;
157 /// let bufs: &[_] = &[
158 /// IoSlice::new(b"hello"),
159 /// IoSlice::new(b" "),
160 /// IoSlice::new(b"world"),
163 /// file.write_vectored(&bufs).await?;
169 /// [`write`]: AsyncWriteExt::write
170 fn write_vectored
<'a
, 'b
>(&'a
mut self, bufs
: &'a
[IoSlice
<'b
>]) -> WriteVectored
<'a
, 'b
, Self>
174 write_vectored(self, bufs
)
177 /// Writes a buffer into this writer, advancing the buffer's internal
183 /// async fn write_buf<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize>;
186 /// This function will attempt to write the entire contents of `buf`, but
187 /// the entire write may not succeed, or the write may also generate an
188 /// error. After the operation completes, the buffer's
189 /// internal cursor is advanced by the number of bytes written. A
190 /// subsequent call to `write_buf` using the **same** `buf` value will
191 /// resume from the point that the first call to `write_buf` completed.
192 /// A call to `write_buf` represents *at most one* attempt to write to any
197 /// If the return value is `Ok(n)` then it must be guaranteed that `n <=
198 /// buf.len()`. A return value of `0` typically means that the
199 /// underlying object is no longer able to accept bytes and will likely
200 /// not be able to in the future as well, or that the buffer provided is
205 /// Each call to `write` may generate an I/O error indicating that the
206 /// operation could not be completed. If an error is returned then no bytes
207 /// in the buffer were written to this writer.
209 /// It is **not** considered an error if the entire buffer could not be
210 /// written to this writer.
214 /// This method is cancellation safe in the sense that if it is used as
215 /// the event in a [`tokio::select!`](crate::select) statement and some
216 /// other branch completes first, then it is guaranteed that no data was
217 /// written to this `AsyncWrite`.
221 /// [`File`] implements [`AsyncWrite`] and [`Cursor`]`<&[u8]>` implements [`Buf`]:
223 /// [`File`]: crate::fs::File
224 /// [`Buf`]: bytes::Buf
225 /// [`Cursor`]: std::io::Cursor
228 /// use tokio::io::{self, AsyncWriteExt};
229 /// use tokio::fs::File;
232 /// use std::io::Cursor;
235 /// async fn main() -> io::Result<()> {
236 /// let mut file = File::create("foo.txt").await?;
237 /// let mut buffer = Cursor::new(b"data to write");
239 /// // Loop until the entire contents of the buffer are written to
241 /// while buffer.has_remaining() {
242 /// // Writes some prefix of the byte string, not necessarily
244 /// file.write_buf(&mut buffer).await?;
250 fn write_buf
<'a
, B
>(&'a
mut self, src
: &'a
mut B
) -> WriteBuf
<'a
, Self, B
>
258 /// Attempts to write an entire buffer into this writer
263 /// async fn write_all_buf(&mut self, buf: impl Buf) -> Result<(), io::Error> {
264 /// while buf.has_remaining() {
265 /// self.write_buf(&mut buf).await?;
271 /// This method will continuously call [`write`] until
272 /// [`buf.has_remaining()`](bytes::Buf::has_remaining) returns false. This method will not
273 /// return until the entire buffer has been successfully written or an error occurs. The
274 /// first error generated will be returned.
276 /// The buffer is advanced after each chunk is successfully written. After failure,
277 /// `src.chunk()` will return the chunk that failed to write.
281 /// If `write_all_buf` is used as the event in a
282 /// [`tokio::select!`](crate::select) statement and some other branch
283 /// completes first, then the data in the provided buffer may have been
284 /// partially written. However, it is guaranteed that the provided
285 /// buffer has been [advanced] by the amount of bytes that have been
286 /// partially written.
290 /// [`File`] implements [`AsyncWrite`] and [`Cursor`]`<&[u8]>` implements [`Buf`]:
292 /// [`File`]: crate::fs::File
293 /// [`Buf`]: bytes::Buf
294 /// [`Cursor`]: std::io::Cursor
295 /// [advanced]: bytes::Buf::advance
298 /// use tokio::io::{self, AsyncWriteExt};
299 /// use tokio::fs::File;
301 /// use std::io::Cursor;
304 /// async fn main() -> io::Result<()> {
305 /// let mut file = File::create("foo.txt").await?;
306 /// let mut buffer = Cursor::new(b"data to write");
308 /// file.write_all_buf(&mut buffer).await?;
313 /// [`write`]: AsyncWriteExt::write
314 fn write_all_buf
<'a
, B
>(&'a
mut self, src
: &'a
mut B
) -> WriteAllBuf
<'a
, Self, B
>
319 write_all_buf(self, src
)
322 /// Attempts to write an entire buffer into this writer.
327 /// async fn write_all(&mut self, buf: &[u8]) -> io::Result<()>;
330 /// This method will continuously call [`write`] until there is no more data
331 /// to be written. This method will not return until the entire buffer
332 /// has been successfully written or such an error occurs. The first
333 /// error generated from this method will be returned.
337 /// This method is not cancellation safe. If it is used as the event
338 /// in a [`tokio::select!`](crate::select) statement and some other
339 /// branch completes first, then the provided buffer may have been
340 /// partially written, but future calls to `write_all` will start over
341 /// from the beginning of the buffer.
345 /// This function will return the first error that [`write`] returns.
350 /// use tokio::io::{self, AsyncWriteExt};
351 /// use tokio::fs::File;
354 /// async fn main() -> io::Result<()> {
355 /// let mut buffer = File::create("foo.txt").await?;
357 /// buffer.write_all(b"some bytes").await?;
362 /// [`write`]: AsyncWriteExt::write
363 fn write_all
<'a
>(&'a
mut self, src
: &'a
[u8]) -> WriteAll
<'a
, Self>
371 /// Writes an unsigned 8-bit integer to the underlying writer.
376 /// async fn write_u8(&mut self, n: u8) -> io::Result<()>;
379 /// It is recommended to use a buffered writer to avoid excessive
384 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
386 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
390 /// Write unsigned 8 bit integers to a `AsyncWrite`:
393 /// use tokio::io::{self, AsyncWriteExt};
396 /// async fn main() -> io::Result<()> {
397 /// let mut writer = Vec::new();
399 /// writer.write_u8(2).await?;
400 /// writer.write_u8(5).await?;
402 /// assert_eq!(writer, b"\x02\x05");
406 fn write_u8(&mut self, n
: u8) -> WriteU8
;
408 /// Writes an unsigned 8-bit integer to the underlying writer.
413 /// async fn write_i8(&mut self, n: i8) -> io::Result<()>;
416 /// It is recommended to use a buffered writer to avoid excessive
421 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
423 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
427 /// Write unsigned 8 bit integers to a `AsyncWrite`:
430 /// use tokio::io::{self, AsyncWriteExt};
433 /// async fn main() -> io::Result<()> {
434 /// let mut writer = Vec::new();
436 /// writer.write_u8(2).await?;
437 /// writer.write_u8(5).await?;
439 /// assert_eq!(writer, b"\x02\x05");
443 fn write_i8(&mut self, n
: i8) -> WriteI8
;
445 /// Writes an unsigned 16-bit integer in big-endian order to the
446 /// underlying writer.
451 /// async fn write_u16(&mut self, n: u16) -> io::Result<()>;
454 /// It is recommended to use a buffered writer to avoid excessive
459 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
461 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
465 /// Write unsigned 16-bit integers to a `AsyncWrite`:
468 /// use tokio::io::{self, AsyncWriteExt};
471 /// async fn main() -> io::Result<()> {
472 /// let mut writer = Vec::new();
474 /// writer.write_u16(517).await?;
475 /// writer.write_u16(768).await?;
477 /// assert_eq!(writer, b"\x02\x05\x03\x00");
481 fn write_u16(&mut self, n
: u16) -> WriteU16
;
483 /// Writes a signed 16-bit integer in big-endian order to the
484 /// underlying writer.
489 /// async fn write_i16(&mut self, n: i16) -> io::Result<()>;
492 /// It is recommended to use a buffered writer to avoid excessive
497 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
499 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
503 /// Write signed 16-bit integers to a `AsyncWrite`:
506 /// use tokio::io::{self, AsyncWriteExt};
509 /// async fn main() -> io::Result<()> {
510 /// let mut writer = Vec::new();
512 /// writer.write_i16(193).await?;
513 /// writer.write_i16(-132).await?;
515 /// assert_eq!(writer, b"\x00\xc1\xff\x7c");
519 fn write_i16(&mut self, n
: i16) -> WriteI16
;
521 /// Writes an unsigned 32-bit integer in big-endian order to the
522 /// underlying writer.
527 /// async fn write_u32(&mut self, n: u32) -> io::Result<()>;
530 /// It is recommended to use a buffered writer to avoid excessive
535 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
537 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
541 /// Write unsigned 32-bit integers to a `AsyncWrite`:
544 /// use tokio::io::{self, AsyncWriteExt};
547 /// async fn main() -> io::Result<()> {
548 /// let mut writer = Vec::new();
550 /// writer.write_u32(267).await?;
551 /// writer.write_u32(1205419366).await?;
553 /// assert_eq!(writer, b"\x00\x00\x01\x0b\x47\xd9\x3d\x66");
557 fn write_u32(&mut self, n
: u32) -> WriteU32
;
559 /// Writes a signed 32-bit integer in big-endian order to the
560 /// underlying writer.
565 /// async fn write_i32(&mut self, n: i32) -> io::Result<()>;
568 /// It is recommended to use a buffered writer to avoid excessive
573 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
575 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
579 /// Write signed 32-bit integers to a `AsyncWrite`:
582 /// use tokio::io::{self, AsyncWriteExt};
585 /// async fn main() -> io::Result<()> {
586 /// let mut writer = Vec::new();
588 /// writer.write_i32(267).await?;
589 /// writer.write_i32(1205419366).await?;
591 /// assert_eq!(writer, b"\x00\x00\x01\x0b\x47\xd9\x3d\x66");
595 fn write_i32(&mut self, n
: i32) -> WriteI32
;
597 /// Writes an unsigned 64-bit integer in big-endian order to the
598 /// underlying writer.
603 /// async fn write_u64(&mut self, n: u64) -> io::Result<()>;
606 /// It is recommended to use a buffered writer to avoid excessive
611 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
613 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
617 /// Write unsigned 64-bit integers to a `AsyncWrite`:
620 /// use tokio::io::{self, AsyncWriteExt};
623 /// async fn main() -> io::Result<()> {
624 /// let mut writer = Vec::new();
626 /// writer.write_u64(918733457491587).await?;
627 /// writer.write_u64(143).await?;
629 /// assert_eq!(writer, b"\x00\x03\x43\x95\x4d\x60\x86\x83\x00\x00\x00\x00\x00\x00\x00\x8f");
633 fn write_u64(&mut self, n
: u64) -> WriteU64
;
635 /// Writes an signed 64-bit integer in big-endian order to the
636 /// underlying writer.
641 /// async fn write_i64(&mut self, n: i64) -> io::Result<()>;
644 /// It is recommended to use a buffered writer to avoid excessive
649 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
651 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
655 /// Write signed 64-bit integers to a `AsyncWrite`:
658 /// use tokio::io::{self, AsyncWriteExt};
661 /// async fn main() -> io::Result<()> {
662 /// let mut writer = Vec::new();
664 /// writer.write_i64(i64::MIN).await?;
665 /// writer.write_i64(i64::MAX).await?;
667 /// assert_eq!(writer, b"\x80\x00\x00\x00\x00\x00\x00\x00\x7f\xff\xff\xff\xff\xff\xff\xff");
671 fn write_i64(&mut self, n
: i64) -> WriteI64
;
673 /// Writes an unsigned 128-bit integer in big-endian order to the
674 /// underlying writer.
679 /// async fn write_u128(&mut self, n: u128) -> io::Result<()>;
682 /// It is recommended to use a buffered writer to avoid excessive
687 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
689 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
693 /// Write unsigned 128-bit integers to a `AsyncWrite`:
696 /// use tokio::io::{self, AsyncWriteExt};
699 /// async fn main() -> io::Result<()> {
700 /// let mut writer = Vec::new();
702 /// writer.write_u128(16947640962301618749969007319746179).await?;
704 /// assert_eq!(writer, vec![
705 /// 0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83,
706 /// 0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
711 fn write_u128(&mut self, n
: u128
) -> WriteU128
;
713 /// Writes an signed 128-bit integer in big-endian order to the
714 /// underlying writer.
719 /// async fn write_i128(&mut self, n: i128) -> io::Result<()>;
722 /// It is recommended to use a buffered writer to avoid excessive
727 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
729 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
733 /// Write signed 128-bit integers to a `AsyncWrite`:
736 /// use tokio::io::{self, AsyncWriteExt};
739 /// async fn main() -> io::Result<()> {
740 /// let mut writer = Vec::new();
742 /// writer.write_i128(i128::MIN).await?;
744 /// assert_eq!(writer, vec![
745 /// 0x80, 0, 0, 0, 0, 0, 0, 0,
746 /// 0, 0, 0, 0, 0, 0, 0, 0
751 fn write_i128(&mut self, n
: i128
) -> WriteI128
;
754 /// Writes an unsigned 16-bit integer in little-endian order to the
755 /// underlying writer.
760 /// async fn write_u16_le(&mut self, n: u16) -> io::Result<()>;
763 /// It is recommended to use a buffered writer to avoid excessive
768 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
770 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
774 /// Write unsigned 16-bit integers to a `AsyncWrite`:
777 /// use tokio::io::{self, AsyncWriteExt};
780 /// async fn main() -> io::Result<()> {
781 /// let mut writer = Vec::new();
783 /// writer.write_u16_le(517).await?;
784 /// writer.write_u16_le(768).await?;
786 /// assert_eq!(writer, b"\x05\x02\x00\x03");
790 fn write_u16_le(&mut self, n
: u16) -> WriteU16Le
;
792 /// Writes a signed 16-bit integer in little-endian order to the
793 /// underlying writer.
798 /// async fn write_i16_le(&mut self, n: i16) -> io::Result<()>;
801 /// It is recommended to use a buffered writer to avoid excessive
806 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
808 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
812 /// Write signed 16-bit integers to a `AsyncWrite`:
815 /// use tokio::io::{self, AsyncWriteExt};
818 /// async fn main() -> io::Result<()> {
819 /// let mut writer = Vec::new();
821 /// writer.write_i16_le(193).await?;
822 /// writer.write_i16_le(-132).await?;
824 /// assert_eq!(writer, b"\xc1\x00\x7c\xff");
828 fn write_i16_le(&mut self, n
: i16) -> WriteI16Le
;
830 /// Writes an unsigned 32-bit integer in little-endian order to the
831 /// underlying writer.
836 /// async fn write_u32_le(&mut self, n: u32) -> io::Result<()>;
839 /// It is recommended to use a buffered writer to avoid excessive
844 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
846 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
850 /// Write unsigned 32-bit integers to a `AsyncWrite`:
853 /// use tokio::io::{self, AsyncWriteExt};
856 /// async fn main() -> io::Result<()> {
857 /// let mut writer = Vec::new();
859 /// writer.write_u32_le(267).await?;
860 /// writer.write_u32_le(1205419366).await?;
862 /// assert_eq!(writer, b"\x0b\x01\x00\x00\x66\x3d\xd9\x47");
866 fn write_u32_le(&mut self, n
: u32) -> WriteU32Le
;
868 /// Writes a signed 32-bit integer in little-endian order to the
869 /// underlying writer.
874 /// async fn write_i32_le(&mut self, n: i32) -> io::Result<()>;
877 /// It is recommended to use a buffered writer to avoid excessive
882 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
884 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
888 /// Write signed 32-bit integers to a `AsyncWrite`:
891 /// use tokio::io::{self, AsyncWriteExt};
894 /// async fn main() -> io::Result<()> {
895 /// let mut writer = Vec::new();
897 /// writer.write_i32_le(267).await?;
898 /// writer.write_i32_le(1205419366).await?;
900 /// assert_eq!(writer, b"\x0b\x01\x00\x00\x66\x3d\xd9\x47");
904 fn write_i32_le(&mut self, n
: i32) -> WriteI32Le
;
906 /// Writes an unsigned 64-bit integer in little-endian order to the
907 /// underlying writer.
912 /// async fn write_u64_le(&mut self, n: u64) -> io::Result<()>;
915 /// It is recommended to use a buffered writer to avoid excessive
920 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
922 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
926 /// Write unsigned 64-bit integers to a `AsyncWrite`:
929 /// use tokio::io::{self, AsyncWriteExt};
932 /// async fn main() -> io::Result<()> {
933 /// let mut writer = Vec::new();
935 /// writer.write_u64_le(918733457491587).await?;
936 /// writer.write_u64_le(143).await?;
938 /// assert_eq!(writer, b"\x83\x86\x60\x4d\x95\x43\x03\x00\x8f\x00\x00\x00\x00\x00\x00\x00");
942 fn write_u64_le(&mut self, n
: u64) -> WriteU64Le
;
944 /// Writes an signed 64-bit integer in little-endian order to the
945 /// underlying writer.
950 /// async fn write_i64_le(&mut self, n: i64) -> io::Result<()>;
953 /// It is recommended to use a buffered writer to avoid excessive
958 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
960 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
964 /// Write signed 64-bit integers to a `AsyncWrite`:
967 /// use tokio::io::{self, AsyncWriteExt};
970 /// async fn main() -> io::Result<()> {
971 /// let mut writer = Vec::new();
973 /// writer.write_i64_le(i64::MIN).await?;
974 /// writer.write_i64_le(i64::MAX).await?;
976 /// assert_eq!(writer, b"\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff\xff\xff\xff\xff\x7f");
980 fn write_i64_le(&mut self, n
: i64) -> WriteI64Le
;
982 /// Writes an unsigned 128-bit integer in little-endian order to the
983 /// underlying writer.
988 /// async fn write_u128_le(&mut self, n: u128) -> io::Result<()>;
991 /// It is recommended to use a buffered writer to avoid excessive
996 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
998 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
1002 /// Write unsigned 128-bit integers to a `AsyncWrite`:
1005 /// use tokio::io::{self, AsyncWriteExt};
1008 /// async fn main() -> io::Result<()> {
1009 /// let mut writer = Vec::new();
1011 /// writer.write_u128_le(16947640962301618749969007319746179).await?;
1013 /// assert_eq!(writer, vec![
1014 /// 0x83, 0x86, 0x60, 0x4d, 0x95, 0x43, 0x03, 0x00,
1015 /// 0x83, 0x86, 0x60, 0x4d, 0x95, 0x43, 0x03, 0x00,
1020 fn write_u128_le(&mut self, n
: u128
) -> WriteU128Le
;
1022 /// Writes an signed 128-bit integer in little-endian order to the
1023 /// underlying writer.
1028 /// async fn write_i128_le(&mut self, n: i128) -> io::Result<()>;
1031 /// It is recommended to use a buffered writer to avoid excessive
1036 /// This method returns the same errors as [`AsyncWriteExt::write_all`].
1038 /// [`AsyncWriteExt::write_all`]: AsyncWriteExt::write_all
1042 /// Write signed 128-bit integers to a `AsyncWrite`:
1045 /// use tokio::io::{self, AsyncWriteExt};
1048 /// async fn main() -> io::Result<()> {
1049 /// let mut writer = Vec::new();
1051 /// writer.write_i128_le(i128::MIN).await?;
1053 /// assert_eq!(writer, vec![
1054 /// 0, 0, 0, 0, 0, 0, 0,
1055 /// 0, 0, 0, 0, 0, 0, 0, 0, 0x80
1060 fn write_i128_le(&mut self, n
: i128
) -> WriteI128Le
;
1063 /// Flushes this output stream, ensuring that all intermediately buffered
1064 /// contents reach their destination.
1069 /// async fn flush(&mut self) -> io::Result<()>;
1074 /// It is considered an error if not all bytes could be written due to
1075 /// I/O errors or EOF being reached.
1080 /// use tokio::io::{self, BufWriter, AsyncWriteExt};
1081 /// use tokio::fs::File;
1084 /// async fn main() -> io::Result<()> {
1085 /// let f = File::create("foo.txt").await?;
1086 /// let mut buffer = BufWriter::new(f);
1088 /// buffer.write_all(b"some bytes").await?;
1089 /// buffer.flush().await?;
1093 fn flush(&mut self) -> Flush
<'_
, Self>
1100 /// Shuts down the output stream, ensuring that the value can be dropped
1106 /// async fn shutdown(&mut self) -> io::Result<()>;
1109 /// Similar to [`flush`], all intermediately buffered is written to the
1110 /// underlying stream. Once the operation completes, the caller should
1111 /// no longer attempt to write to the stream. For example, the
1112 /// `TcpStream` implementation will issue a `shutdown(Write)` sys call.
1114 /// [`flush`]: fn@crate::io::AsyncWriteExt::flush
1119 /// use tokio::io::{self, BufWriter, AsyncWriteExt};
1120 /// use tokio::fs::File;
1123 /// async fn main() -> io::Result<()> {
1124 /// let f = File::create("foo.txt").await?;
1125 /// let mut buffer = BufWriter::new(f);
1127 /// buffer.write_all(b"some bytes").await?;
1128 /// buffer.shutdown().await?;
1132 fn shutdown(&mut self) -> Shutdown
<'_
, Self>
1141 impl<W
: AsyncWrite
+ ?Sized
> AsyncWriteExt
for W {}