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
use async_trait::async_trait;

use crate::{
    datagram,
    io::{Read, Write},
    utils::maybe,
};

pub trait Streams: maybe::Send {
    type SendStream: Write;
    type RecvStream: Read;
}

pub type BiStreamsFor<T> = (<T as Streams>::SendStream, <T as Streams>::RecvStream);

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait OpeningBiStream: maybe::Send {
    type Streams: Streams;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn wait_bi(self) -> Result<BiStreamsFor<Self::Streams>, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait OpenBiStream: Streams {
    type Opening: OpeningBiStream<Streams = Self>;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn open_bi(&self) -> Result<Self::Opening, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait AcceptBiStream: Streams {
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn accept_bi(&self) -> Result<BiStreamsFor<Self>, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait OpeningUniStream: maybe::Send {
    type Streams: Streams;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn wait_uni(self) -> Result<<Self::Streams as Streams>::SendStream, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait OpenUniStream: Streams {
    type Opening: OpeningUniStream;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn open_uni(&self) -> Result<Self::Opening, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait AcceptUniStream: Streams {
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn accept_uni(&self) -> Result<Self::RecvStream, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait EndpointConnect: Sized + maybe::Send {
    type Connecting: Connecting;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn connect(&self, url: &str) -> Result<Self::Connecting, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait Connecting: maybe::Send {
    type Connection: maybe::Send;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn wait_connect(self) -> Result<Self::Connection, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait EndpointAccept: Sized + maybe::Send {
    type Accepting: Accepting;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn accept(&self) -> Result<Option<Self::Accepting>, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait Accepting: maybe::Send {
    type Request: Request;
    type Error: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn wait_accept(self) -> Result<Self::Request, Self::Error>;
}

#[cfg_attr(not(target_family = "wasm"), async_trait)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
pub trait Request: maybe::Send {
    type Connection: maybe::Send;
    type OkError: std::error::Error + maybe::Send + maybe::Sync + 'static;
    type CloseError: std::error::Error + maybe::Send + maybe::Sync + 'static;

    async fn ok(self) -> Result<Self::Connection, Self::OkError>;
    async fn close(self, status: u16) -> Result<(), Self::CloseError>;
}

pub trait Connection:
    Streams + OpenBiStream + OpenUniStream + AcceptBiStream + AcceptUniStream + datagram::Datagrams
{
}

impl<T> Connection for T where
    T: Streams
        + OpenBiStream
        + OpenUniStream
        + AcceptBiStream
        + AcceptUniStream
        + datagram::Datagrams
{
}