async_coap/send_desc/
include_socket_addr.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::*;
17
18impl<SD: SendDescUnicast> SendDescUnicast for IncludeSocketAddr<SD> {}
19impl<SD: SendDescMulticast> SendDescMulticast for IncludeSocketAddr<SD> {}
20
21/// Combinator for Send Descriptors created by [`SendDescExt::include_socket_addr`].
22#[derive(Debug)]
23pub struct IncludeSocketAddr<SD> {
24    pub(super) inner: SD,
25}
26
27impl<SD> IncludeSocketAddr<SD> {
28    pub(super) fn new(inner: SD) -> IncludeSocketAddr<SD> {
29        IncludeSocketAddr { inner }
30    }
31}
32
33impl<SD, IC, R> SendDesc<IC, (R, IC::SocketAddr)> for IncludeSocketAddr<SD>
34where
35    SD: SendDesc<IC, R> + Send,
36    IC: InboundContext,
37    R: Send,
38{
39    send_desc_passthru_timing!(inner);
40    send_desc_passthru_options!(inner);
41    send_desc_passthru_payload!(inner);
42    send_desc_passthru_supports_option!(inner);
43
44    fn handler(
45        &mut self,
46        context: Result<&IC, Error>,
47    ) -> Result<ResponseStatus<(R, IC::SocketAddr)>, Error> {
48        let socket_addr = context.ok().map(|x| x.remote_socket_addr());
49
50        self.inner.handler(context).map(|x| match (x, socket_addr) {
51            (ResponseStatus::Done(x), Some(socket_addr)) => ResponseStatus::Done((x, socket_addr)),
52            (ResponseStatus::Done(_), None) => unreachable!(),
53            (ResponseStatus::SendNext, _) => ResponseStatus::SendNext,
54            (ResponseStatus::Continue, _) => ResponseStatus::Continue,
55        })
56    }
57}