rd_std/builtin/
combine.rs

1use futures::future::BoxFuture;
2use rd_interface::{
3    registry::{EmptyConfig, NetFactory},
4    Address, Context, INet, Result, TcpListener, TcpStream, UdpSocket,
5};
6
7pub struct CombineNet(rd_interface::Net, rd_interface::Net, rd_interface::Net);
8
9impl CombineNet {
10    fn new(mut nets: Vec<rd_interface::Net>) -> rd_interface::Result<CombineNet> {
11        if nets.len() != 3 {
12            return Err(rd_interface::Error::Other(
13                "Must have one net".to_string().into(),
14            ));
15        }
16        let net0 = nets.remove(0);
17        let net1 = nets.remove(0);
18        let net2 = nets.remove(0);
19        Ok(CombineNet(net0, net1, net2))
20    }
21}
22
23impl INet for CombineNet {
24    #[inline(always)]
25    fn tcp_connect<'life0: 'a, 'life1: 'a, 'a>(
26        &'life0 self,
27        ctx: &'life1 mut Context,
28        addr: Address,
29    ) -> BoxFuture<'a, Result<TcpStream>>
30    where
31        Self: 'a,
32    {
33        self.0.tcp_connect(ctx, addr)
34    }
35
36    #[inline(always)]
37    fn tcp_bind<'life0: 'a, 'life1: 'a, 'a>(
38        &'life0 self,
39        ctx: &'life1 mut Context,
40        addr: Address,
41    ) -> BoxFuture<'a, Result<TcpListener>>
42    where
43        Self: 'a,
44    {
45        self.1.tcp_bind(ctx, addr)
46    }
47
48    #[inline(always)]
49    fn udp_bind<'life0: 'a, 'life1: 'a, 'a>(
50        &'life0 self,
51        ctx: &'life1 mut Context,
52        addr: Address,
53    ) -> BoxFuture<'a, Result<UdpSocket>>
54    where
55        Self: 'a,
56    {
57        self.2.udp_bind(ctx, addr)
58    }
59}
60
61impl NetFactory for CombineNet {
62    const NAME: &'static str = "combine";
63    type Config = EmptyConfig;
64    type Net = Self;
65
66    fn new(nets: Vec<rd_interface::Net>, _config: Self::Config) -> Result<Self> {
67        CombineNet::new(nets)
68    }
69}