async_coap/datagram/
null_socket.rs

1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use super::*;
17use futures::task::Context;
18use futures::Poll;
19use std::fmt::{Debug, Display, Formatter};
20use std::pin::Pin;
21
22/// Simplified "SocketAddr" for [`NullSocket`]. Does nothing.
23#[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/// An instance of [`AsyncDatagramSocket`] that implements a simple null interface, where
79/// all packets that are sent are discarded.
80#[derive(Debug)]
81pub struct NullSocket;
82
83impl NullSocket {
84    /// Creates a new instance of [`NullSocket`].
85    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}