async_coap/
response_status.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
16/// Successful return type from [send descriptor handler method](send_desc/trait.SendDesc.html#tymethod.handler)
17/// that indicates what should happen next.
18#[derive(Debug, Copy, Eq, PartialEq, Clone)]
19pub enum ResponseStatus<T = ()> {
20    /// Emit the given value.
21    Done(T),
22
23    /// Allocate a new Message ID, resend a new request, and wait for the associated response.
24    ///
25    /// This is used when handling block requests to fetch additional blocks, among other cases.
26    SendNext,
27
28    /// Wait for additional responses to the original request without sending new requests.
29    ///
30    /// This is used when handling multicast requests and observing.
31    Continue,
32}
33
34impl<T> ResponseStatus<T> {
35    /// If the response status is `Done(value)`, returns `Some(value)`, otherwise returns `None`.
36    pub fn done(self) -> Option<T> {
37        match self {
38            ResponseStatus::Done(x) => Some(x),
39            _ => None,
40        }
41    }
42
43    /// Returns true if the response status is `Done(...)`, false otherwise.
44    pub fn is_done(&self) -> bool {
45        match *self {
46            ResponseStatus::Done(_) => true,
47            _ => false,
48        }
49    }
50
51    /// Returns true if the response status is `SendNext`, false otherwise.
52    pub fn is_send_next(&self) -> bool {
53        match *self {
54            ResponseStatus::SendNext => true,
55            _ => false,
56        }
57    }
58
59    /// Returns true if the response status is `Continue`, false otherwise.
60    pub fn is_continue(&self) -> bool {
61        match *self {
62            ResponseStatus::Continue => true,
63            _ => false,
64        }
65    }
66
67    /// Converts the contained type to be a reference, so that `Done(T)` becomes `Done(&T)`.
68    pub fn as_ref(&self) -> ResponseStatus<&T> {
69        match *self {
70            ResponseStatus::Done(ref x) => ResponseStatus::Done(x),
71            ResponseStatus::SendNext => ResponseStatus::SendNext,
72            ResponseStatus::Continue => ResponseStatus::Continue,
73        }
74    }
75
76    /// Converts the contained type to be a mutable reference, so that `Done(T)` becomes
77    /// `Done(&mut T)`.
78    pub fn as_mut(&mut self) -> ResponseStatus<&mut T> {
79        match *self {
80            ResponseStatus::Done(ref mut x) => ResponseStatus::Done(x),
81            ResponseStatus::SendNext => ResponseStatus::SendNext,
82            ResponseStatus::Continue => ResponseStatus::Continue,
83        }
84    }
85}