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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::prelude::*;
use crate::IOStream;
use crate::InputStreamAsyncRead;
use crate::OutputStreamAsyncWrite;
use crate::PollableInputStream;
use crate::PollableOutputStream;
use futures_core::task::{Context, Poll};
use futures_io::{AsyncRead, AsyncWrite};
use glib::object::{Cast, IsA};
use std::io;
use std::pin::Pin;
pub trait IOStreamExtManual: Sized + IsA<IOStream> {
fn into_async_read_write(self) -> Result<IOStreamAsyncReadWrite<Self>, Self> {
let write = self
.output_stream()
.dynamic_cast::<PollableOutputStream>()
.ok()
.and_then(|s| s.into_async_write().ok());
let read = self
.input_stream()
.dynamic_cast::<PollableInputStream>()
.ok()
.and_then(|s| s.into_async_read().ok());
let (read, write) = match (read, write) {
(Some(read), Some(write)) => (read, write),
_ => return Err(self),
};
Ok(IOStreamAsyncReadWrite {
io_stream: self,
read,
write,
})
}
}
impl<O: IsA<IOStream>> IOStreamExtManual for O {}
#[derive(Debug)]
pub struct IOStreamAsyncReadWrite<T> {
io_stream: T,
read: InputStreamAsyncRead<PollableInputStream>,
write: OutputStreamAsyncWrite<PollableOutputStream>,
}
impl<T: IsA<IOStream>> IOStreamAsyncReadWrite<T> {
pub fn input_stream(&self) -> &PollableInputStream {
self.read.input_stream()
}
pub fn output_stream(&self) -> &PollableOutputStream {
self.write.output_stream()
}
pub fn into_io_stream(self) -> T {
self.io_stream
}
pub fn io_stream(&self) -> &T {
&self.io_stream
}
}
impl<T: IsA<IOStream> + std::marker::Unpin> AsyncRead for IOStreamAsyncReadWrite<T> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
Pin::new(&mut Pin::get_mut(self).read).poll_read(cx, buf)
}
}
impl<T: IsA<IOStream> + std::marker::Unpin> AsyncWrite for IOStreamAsyncReadWrite<T> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
Pin::new(&mut Pin::get_mut(self).write).poll_write(cx, buf)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Pin::new(&mut Pin::get_mut(self).write).poll_close(cx)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Pin::new(&mut Pin::get_mut(self).write).poll_flush(cx)
}
}