1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/// A trait to determine if the value is left unspecified,
/// usually containing the default value.
///
/// See: <https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address>
pub trait Unspecified: Sized {
    /// Returns true if the value is unspecified
    fn is_unspecified(&self) -> bool;

    /// Coerce a potentially unspecified value into an Option<Self>
    fn filter_unspecified(self) -> Option<Self> {
        if self.is_unspecified() {
            None
        } else {
            Some(self)
        }
    }
}

macro_rules! unspecified_integer {
    ($name:ident) => {
        impl Unspecified for s2n_codec::zerocopy::$name {
            fn is_unspecified(&self) -> bool {
                Self::default().eq(self)
            }
        }
    };
}

unspecified_integer!(U16);
unspecified_integer!(U32);
unspecified_integer!(U64);
unspecified_integer!(U128);