async_hal/io/
async_write.rs1use core::{
2 ops::DerefMut,
3 pin::Pin,
4 task::{Context, Poll},
5};
6use void::Void;
7
8use super::{write_all::write_all, WriteAll};
9
10pub trait AsyncWrite {
11 type Error;
12
13 fn poll_write(
14 self: Pin<&mut Self>,
15 cx: &mut Context,
16 buf: &[u8],
17 ) -> Poll<Result<usize, Self::Error>>;
18
19 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>>;
20
21 fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self>
47 where
48 Self: Unpin,
49 {
50 write_all(self, buf)
51 }
52}
53
54impl<T: ?Sized + AsyncWrite + Unpin> AsyncWrite for &mut T {
55 type Error = T::Error;
56
57 fn poll_write(
58 mut self: Pin<&mut Self>,
59 cx: &mut Context,
60 buf: &[u8],
61 ) -> Poll<Result<usize, Self::Error>> {
62 Pin::new(&mut **self).poll_write(cx, buf)
63 }
64
65 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
66 Pin::new(&mut **self).poll_flush(cx)
67 }
68}
69
70impl<P> AsyncWrite for Pin<P>
71where
72 P: DerefMut + Unpin,
73 P::Target: AsyncWrite,
74{
75 type Error = <P::Target as AsyncWrite>::Error;
76
77 fn poll_write(
78 self: Pin<&mut Self>,
79 cx: &mut Context<'_>,
80 buf: &[u8],
81 ) -> Poll<Result<usize, Self::Error>> {
82 self.get_mut().as_mut().poll_write(cx, buf)
83 }
84
85 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
86 self.get_mut().as_mut().poll_flush(cx)
87 }
88}
89
90impl AsyncWrite for &'_ mut [u8] {
91 type Error = Void;
92
93 fn poll_write(
94 mut self: Pin<&mut Self>,
95 _cx: &mut Context,
96 buf: &[u8],
97 ) -> Poll<Result<usize, Self::Error>> {
98 let amt = core::cmp::min(buf.len(), self.len());
99 let (a, b) = core::mem::replace(&mut *self, &mut []).split_at_mut(amt);
100 a.copy_from_slice(&buf[..amt]);
101 *self = b;
102 Poll::Ready(Ok(amt))
103 }
104
105 fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
106 Poll::Ready(Ok(()))
107 }
108}