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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::Result;
use core::task::{Context, Poll};

pub trait Connection {
    fn id(&self) -> u64;
    fn poll_open_bidirectional_stream(&mut self, id: u64, cx: &mut Context) -> Poll<Result<()>>;
    fn poll_open_send_stream(&mut self, id: u64, cx: &mut Context) -> Poll<Result<()>>;
    fn poll_accept_stream(&mut self, cx: &mut Context) -> Poll<Result<Option<u64>>>;
    fn poll_send(
        &mut self,
        owner: Owner,
        id: u64,
        bytes: u64,
        cx: &mut Context,
    ) -> Poll<Result<u64>>;
    fn poll_receive(
        &mut self,
        owner: Owner,
        id: u64,
        bytes: u64,
        cx: &mut Context,
    ) -> Poll<Result<u64>>;
    fn poll_send_finish(&mut self, owner: Owner, id: u64, cx: &mut Context) -> Poll<Result<()>>;
    fn poll_receive_finish(&mut self, owner: Owner, id: u64, cx: &mut Context) -> Poll<Result<()>>;
    fn poll_progress(&mut self, cx: &mut Context) -> Poll<Result<()>> {
        let _ = cx;
        Ok(()).into()
    }
    fn poll_finish(&mut self, cx: &mut Context) -> Poll<Result<()>> {
        let _ = cx;
        Ok(()).into()
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Owner {
    Local,
    Remote,
}

impl<T> core::ops::Index<Owner> for [T; 2] {
    type Output = T;

    fn index(&self, index: Owner) -> &Self::Output {
        match index {
            Owner::Local => &self[0],
            Owner::Remote => &self[1],
        }
    }
}

impl<T> core::ops::IndexMut<Owner> for [T; 2] {
    fn index_mut(&mut self, index: Owner) -> &mut Self::Output {
        match index {
            Owner::Local => &mut self[0],
            Owner::Remote => &mut self[1],
        }
    }
}

impl core::ops::Not for Owner {
    type Output = Self;

    fn not(self) -> Self::Output {
        match self {
            Self::Local => Self::Remote,
            Self::Remote => Self::Local,
        }
    }
}