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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use core::{
    task::{Context, Poll},
    time::Duration,
};
use std::io::Error as IoError;

use async_trait::async_trait;
use ssh2::{BlockDirections, Error as Ssh2Error, Session};

use crate::error::Error;

//
#[cfg(feature = "async-io")]
mod impl_async_io;
#[cfg(feature = "tokio")]
mod impl_tokio;

//
#[async_trait]
pub trait AsyncSessionStream {
    //
    async fn x_with<R>(
        &self,
        op: impl FnMut() -> Result<R, Ssh2Error> + Send,
        sess: &Session,
        expected_block_directions: BlockDirections,
        sleep_dur: Option<Duration>,
    ) -> Result<R, Error>;

    async fn rw_with<R>(
        &self,
        op: impl FnMut() -> Result<R, Ssh2Error> + Send,
        sess: &Session,
    ) -> Result<R, Error> {
        self.x_with(
            op,
            sess,
            BlockDirections::Both,
            Some(Duration::from_millis(1)),
        )
        .await
    }

    async fn none_with<R>(
        &self,
        op: impl FnMut() -> Result<R, Ssh2Error> + Send,
        sess: &Session,
    ) -> Result<R, Error> {
        self.x_with(
            op,
            sess,
            BlockDirections::None,
            Some(Duration::from_millis(1)),
        )
        .await
    }

    async fn read_with<R>(
        &self,
        op: impl FnMut() -> Result<R, Ssh2Error> + Send,
        sess: &Session,
    ) -> Result<R, Error> {
        self.x_with(
            op,
            sess,
            BlockDirections::Inbound,
            Some(Duration::from_millis(1)),
        )
        .await
    }

    async fn write_with<R>(
        &self,
        op: impl FnMut() -> Result<R, Ssh2Error> + Send,
        sess: &Session,
    ) -> Result<R, Error> {
        self.x_with(
            op,
            sess,
            BlockDirections::Outbound,
            Some(Duration::from_millis(1)),
        )
        .await
    }

    //
    fn poll_x_with<R>(
        &self,
        cx: &mut Context,
        op: impl FnMut() -> Result<R, IoError> + Send,
        sess: &Session,
        expected_block_directions: BlockDirections,
        sleep_dur: Option<Duration>,
    ) -> Poll<Result<R, IoError>>;

    fn poll_read_with<R>(
        &self,
        cx: &mut Context,
        op: impl FnMut() -> Result<R, IoError> + Send,
        sess: &Session,
    ) -> Poll<Result<R, IoError>> {
        self.poll_x_with(
            cx,
            op,
            sess,
            BlockDirections::Inbound,
            Some(Duration::from_millis(1)),
        )
    }

    fn poll_write_with<R>(
        &self,
        cx: &mut Context,
        op: impl FnMut() -> Result<R, IoError> + Send,
        sess: &Session,
    ) -> Poll<Result<R, IoError>> {
        self.poll_x_with(
            cx,
            op,
            sess,
            BlockDirections::Both,
            Some(Duration::from_millis(1)),
        )
    }
}

//
pub trait BlockDirectionsExt {
    fn is_readable(&self) -> bool;
    fn is_writable(&self) -> bool;
}
impl BlockDirectionsExt for BlockDirections {
    fn is_readable(&self) -> bool {
        matches!(self, BlockDirections::Inbound | BlockDirections::Both)
    }

    fn is_writable(&self) -> bool {
        matches!(self, BlockDirections::Outbound | BlockDirections::Both)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_block_directions_ext() {
        assert!(!BlockDirections::None.is_readable());
        assert!(!BlockDirections::None.is_writable());
        assert!(BlockDirections::Inbound.is_readable());
        assert!(!BlockDirections::Inbound.is_writable());
        assert!(!BlockDirections::Outbound.is_readable());
        assert!(BlockDirections::Outbound.is_writable());
        assert!(BlockDirections::Both.is_readable());
        assert!(BlockDirections::Both.is_writable());
    }
}