forked from compio-rs/compio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.rs
More file actions
69 lines (55 loc) · 1.53 KB
/
split.rs
File metadata and controls
69 lines (55 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::{io, ops::Deref};
use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use compio_io::{AsyncRead, AsyncWrite};
pub(crate) fn split<T>(stream: &T) -> (ReadHalf<'_, T>, WriteHalf<'_, T>)
where
for<'a> &'a T: AsyncRead + AsyncWrite,
{
(ReadHalf(stream), WriteHalf(stream))
}
/// Borrowed read half.
#[derive(Debug)]
pub struct ReadHalf<'a, T>(&'a T);
impl<T> AsyncRead for ReadHalf<'_, T>
where
for<'a> &'a T: AsyncRead,
{
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
self.0.read(buf).await
}
async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
self.0.read_vectored(buf).await
}
}
impl<T> Deref for ReadHalf<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.0
}
}
/// Borrowed write half.
#[derive(Debug)]
pub struct WriteHalf<'a, T>(&'a T);
impl<T> AsyncWrite for WriteHalf<'_, T>
where
for<'a> &'a T: AsyncWrite,
{
async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> {
(self.0).write(buf).await
}
async fn write_vectored<B: IoVectoredBuf>(&mut self, buf: B) -> BufResult<usize, B> {
(self.0).write_vectored(buf).await
}
async fn flush(&mut self) -> io::Result<()> {
(self.0).flush().await
}
async fn shutdown(&mut self) -> io::Result<()> {
(self.0).shutdown().await
}
}
impl<T> Deref for WriteHalf<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.0
}
}