async_coap/datagram/
null_socket.rs1use super::*;
17use futures::task::Context;
18use futures::Poll;
19use std::fmt::{Debug, Display, Formatter};
20use std::pin::Pin;
21
22#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
24pub struct NullSocketAddr;
25
26impl Display for NullSocketAddr {
27 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
28 <Self as Debug>::fmt(self, f)
29 }
30}
31
32impl SocketAddrExt for NullSocketAddr {
33 fn is_multicast(&self) -> bool {
34 return false;
35 }
36
37 fn port(&self) -> u16 {
38 0
39 }
40
41 fn conforming_to(&self, _local: Self) -> Option<Self> {
42 Some(*self)
43 }
44
45 fn addr_to_string(&self) -> String {
46 "null".to_string()
47 }
48}
49
50impl ToSocketAddrs for NullSocketAddr {
51 type Iter = std::option::IntoIter<Self::SocketAddr>;
52 type SocketAddr = Self;
53 type Error = super::Error;
54
55 fn to_socket_addrs(&self) -> Result<Self::Iter, Self::Error> {
56 Ok(Some(*self).into_iter())
57 }
58}
59
60impl MulticastSocket for NullSocket {
61 type IpAddr = String;
62
63 fn join_multicast<A>(&self, _addr: A) -> Result<(), Self::Error>
64 where
65 A: std::convert::Into<Self::IpAddr>,
66 {
67 Ok(())
68 }
69
70 fn leave_multicast<A>(&self, _addr: A) -> Result<(), Self::Error>
71 where
72 A: std::convert::Into<Self::IpAddr>,
73 {
74 Ok(())
75 }
76}
77
78#[derive(Debug)]
81pub struct NullSocket;
82
83impl NullSocket {
84 pub fn new() -> NullSocket {
86 NullSocket
87 }
88}
89
90impl Unpin for NullSocket {}
91
92impl AsyncDatagramSocket for NullSocket {}
93
94impl DatagramSocketTypes for NullSocket {
95 type SocketAddr = NullSocketAddr;
96 type Error = super::Error;
97
98 fn local_addr(&self) -> Result<Self::SocketAddr, Self::Error> {
99 Ok(NullSocketAddr)
100 }
101
102 fn lookup_host(
103 _host: &str,
104 _port: u16,
105 ) -> Result<std::vec::IntoIter<Self::SocketAddr>, Self::Error>
106 where
107 Self: Sized,
108 {
109 Ok(vec![NullSocketAddr].into_iter())
110 }
111}
112
113impl AsyncSendTo for NullSocket {
114 fn poll_send_to<B>(
115 self: Pin<&Self>,
116 _cx: &mut Context<'_>,
117 buf: &[u8],
118 _addr: B,
119 ) -> Poll<Result<usize, Self::Error>>
120 where
121 B: super::ToSocketAddrs<SocketAddr = Self::SocketAddr, Error = Self::Error>,
122 {
123 Poll::Ready(Ok(buf.len()))
124 }
125}
126
127impl AsyncRecvFrom for NullSocket {
128 fn poll_recv_from(
129 self: Pin<&Self>,
130 _cx: &mut Context<'_>,
131 _buf: &mut [u8],
132 ) -> Poll<Result<(usize, Self::SocketAddr, Option<Self::SocketAddr>), Self::Error>> {
133 Poll::Pending
134 }
135}