postcard_rpc/host_client/
test_channels.rs

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
//! A Client implementation using channels for testing

use crate::{
    header::VarSeqKind,
    host_client::{HostClient, WireRx, WireSpawn, WireTx},
    standard_icd::WireError,
};
use core::fmt::Display;
use tokio::sync::mpsc;

/// Create a new HostClient from the given server channels
pub fn new_from_channels(
    tx: mpsc::Sender<Vec<u8>>,
    rx: mpsc::Receiver<Vec<u8>>,
    seq_kind: VarSeqKind,
) -> HostClient<WireError> {
    HostClient::new_with_wire(
        ChannelTx { tx },
        ChannelRx { rx },
        TokSpawn,
        seq_kind,
        crate::standard_icd::ERROR_PATH,
        64,
    )
}

/// Server error kinds
#[derive(Debug)]
pub enum ChannelError {
    /// Rx was closed
    RxClosed,
    /// Tx was closed
    TxClosed,
}

impl Display for ChannelError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        <Self as core::fmt::Debug>::fmt(self, f)
    }
}

impl std::error::Error for ChannelError {}

/// Trait impl for channels
pub struct ChannelRx {
    rx: mpsc::Receiver<Vec<u8>>,
}
/// Trait impl for channels
pub struct ChannelTx {
    tx: mpsc::Sender<Vec<u8>>,
}
/// Trait impl for channels
pub struct ChannelSpawn;

impl WireRx for ChannelRx {
    type Error = ChannelError;

    async fn receive(&mut self) -> Result<Vec<u8>, Self::Error> {
        match self.rx.recv().await {
            Some(v) => {
                #[cfg(test)]
                println!("c<-s: {v:?}");
                Ok(v)
            }
            None => Err(ChannelError::RxClosed),
        }
    }
}

impl WireTx for ChannelTx {
    type Error = ChannelError;

    async fn send(&mut self, data: Vec<u8>) -> Result<(), Self::Error> {
        #[cfg(test)]
        println!("c->s: {data:?}");
        self.tx.send(data).await.map_err(|_| ChannelError::TxClosed)
    }
}

struct TokSpawn;
impl WireSpawn for TokSpawn {
    fn spawn(&mut self, fut: impl std::future::Future<Output = ()> + Send + 'static) {
        _ = tokio::task::spawn(fut);
    }
}