monoio/io/
async_rent_cancelable.rs

1use std::future::Future;
2
3use super::{AsyncReadRent, AsyncWriteRent, CancelHandle};
4use crate::{
5    buf::{IoBuf, IoBufMut, IoVecBuf, IoVecBufMut},
6    BufResult,
7};
8
9/// CancelableAsyncReadRent: async read with a ownership of a buffer and ability to cancel io.
10pub trait CancelableAsyncReadRent: AsyncReadRent {
11    /// Same as read(2)
12    fn cancelable_read<T: IoBufMut>(
13        &mut self,
14        buf: T,
15        c: CancelHandle,
16    ) -> impl Future<Output = BufResult<usize, T>>;
17    /// Same as readv(2)
18    fn cancelable_readv<T: IoVecBufMut>(
19        &mut self,
20        buf: T,
21        c: CancelHandle,
22    ) -> impl Future<Output = BufResult<usize, T>>;
23}
24
25impl<A: ?Sized + CancelableAsyncReadRent> CancelableAsyncReadRent for &mut A {
26    #[inline]
27    fn cancelable_read<T: IoBufMut>(
28        &mut self,
29        buf: T,
30        c: CancelHandle,
31    ) -> impl Future<Output = BufResult<usize, T>> {
32        (**self).cancelable_read(buf, c)
33    }
34
35    #[inline]
36    fn cancelable_readv<T: IoVecBufMut>(
37        &mut self,
38        buf: T,
39        c: CancelHandle,
40    ) -> impl Future<Output = BufResult<usize, T>> {
41        (**self).cancelable_readv(buf, c)
42    }
43}
44
45/// CancelableAsyncWriteRent: async write with a ownership of a buffer and ability to cancel io.
46pub trait CancelableAsyncWriteRent: AsyncWriteRent {
47    /// Same as write(2)
48    fn cancelable_write<T: IoBuf>(
49        &mut self,
50        buf: T,
51        c: CancelHandle,
52    ) -> impl Future<Output = BufResult<usize, T>>;
53
54    /// Same as writev(2)
55    fn cancelable_writev<T: IoVecBuf>(
56        &mut self,
57        buf_vec: T,
58        c: CancelHandle,
59    ) -> impl Future<Output = BufResult<usize, T>>;
60
61    /// Flush buffered data if needed
62    fn cancelable_flush(&mut self, c: CancelHandle) -> impl Future<Output = std::io::Result<()>>;
63
64    /// Same as shutdown
65    fn cancelable_shutdown(&mut self, c: CancelHandle)
66        -> impl Future<Output = std::io::Result<()>>;
67}
68
69impl<A: ?Sized + CancelableAsyncWriteRent> CancelableAsyncWriteRent for &mut A {
70    #[inline]
71    fn cancelable_write<T: IoBuf>(
72        &mut self,
73        buf: T,
74        c: CancelHandle,
75    ) -> impl Future<Output = BufResult<usize, T>> {
76        (**self).cancelable_write(buf, c)
77    }
78
79    #[inline]
80    fn cancelable_writev<T: IoVecBuf>(
81        &mut self,
82        buf_vec: T,
83        c: CancelHandle,
84    ) -> impl Future<Output = BufResult<usize, T>> {
85        (**self).cancelable_writev(buf_vec, c)
86    }
87
88    #[inline]
89    fn cancelable_flush(&mut self, c: CancelHandle) -> impl Future<Output = std::io::Result<()>> {
90        (**self).cancelable_flush(c)
91    }
92
93    #[inline]
94    fn cancelable_shutdown(
95        &mut self,
96        c: CancelHandle,
97    ) -> impl Future<Output = std::io::Result<()>> {
98        (**self).cancelable_shutdown(c)
99    }
100}