network/fundamentals/
endpoint.rs

1use smoltcp::wire;
2
3use crate::{IpAddress, Port};
4
5#[repr(C)]
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7pub struct IpEndpoint {
8    pub address: IpAddress,
9    pub port: Port,
10}
11
12impl IpEndpoint {
13    pub const fn new(address: IpAddress, port: Port) -> Self {
14        Self { address, port }
15    }
16
17    pub const fn into_smoltcp(&self) -> wire::IpEndpoint {
18        wire::IpEndpoint {
19            addr: self.address.into_smoltcp(),
20            port: self.port.into_inner(),
21        }
22    }
23
24    pub const fn from_smoltcp(value: &wire::IpEndpoint) -> Self {
25        Self {
26            address: IpAddress::from_smoltcp(&value.addr),
27            port: Port::from_inner(value.port),
28        }
29    }
30}
31
32#[repr(C)]
33#[derive(Clone, Debug, PartialEq, Eq, Hash)]
34pub struct IpListenEndpoint {
35    pub address: Option<IpAddress>,
36    pub port: Port,
37}
38
39impl IpListenEndpoint {
40    pub const fn new(address: Option<IpAddress>, port: Port) -> Self {
41        Self { address, port }
42    }
43
44    pub const fn into_smoltcp(&self) -> wire::IpListenEndpoint {
45        let smoltcp_address = match &self.address {
46            Some(ip) => Some(ip.into_smoltcp()),
47            None => None,
48        };
49
50        wire::IpListenEndpoint {
51            addr: smoltcp_address,
52            port: self.port.into_inner(),
53        }
54    }
55
56    pub const fn from_smoltcp(value: &wire::IpListenEndpoint) -> Self {
57        let address = match &value.addr {
58            Some(ip) => Some(IpAddress::from_smoltcp(ip)),
59            None => None,
60        };
61
62        Self {
63            address,
64            port: Port::from_inner(value.port),
65        }
66    }
67}