ip/traits/prefix/len.rs
1use core::fmt::{Debug, Display};
2use core::hash::Hash;
3
4use crate::error::Error;
5
6/// Address-family independent interface for IP prefix-lengths
7///
8/// See also [`concrete::PrefixLength<A>`][crate::concrete::PrefixLength] and
9/// [`any::PrefixLength`][crate::any::PrefixLength] for address-family specific
10/// items.
11pub trait Length:
12 Copy + Clone + Debug + Display + Hash + PartialEq + Eq + PartialOrd + 'static
13{
14 /// Returns a new `Self` that is one greater than `self` unless `self` is
15 /// already the maximum possible value.
16 ///
17 /// # Errors
18 ///
19 /// An [`Error`] of kind
20 /// [`Kind::PrefixLength`][crate::error::Kind::PrefixLength] is returned if
21 /// `self` is maximally-valued.
22 ///
23 /// # Examples
24 ///
25 /// ``` rust
26 /// use ip::{
27 /// traits::{Prefix as _, PrefixLength as _},
28 /// Ipv4, Ipv6, Prefix, PrefixLength,
29 /// };
30 ///
31 /// let ipv4_default: Prefix<Ipv4> = "0.0.0.0/0".parse()?;
32 /// let ipv6_host: Prefix<Ipv6> = "2001:db8::1/128".parse()?;
33 ///
34 /// assert_eq!(
35 /// ipv4_default.prefix_len().increment()?,
36 /// PrefixLength::<Ipv4>::from_primitive(1)?,
37 /// );
38 /// assert!(ipv4_default.prefix_len().decrement().is_err());
39 /// assert_eq!(
40 /// ipv6_host.prefix_len().decrement()?,
41 /// PrefixLength::<Ipv6>::from_primitive(127)?,
42 /// );
43 /// assert!(ipv6_host.prefix_len().increment().is_err());
44 /// # Ok::<(), ip::Error>(())
45 /// ```
46 fn increment(self) -> Result<Self, Error>;
47
48 /// Returns a new `Self` that is one less than `self` unless `self` is
49 /// already the minimum possible value.
50 ///
51 /// # Errors
52 ///
53 /// An [`Error`] of kind
54 /// [`Kind::PrefixLength`][crate::error::Kind::PrefixLength] is returned if
55 /// `self` is zero-valued.
56 fn decrement(self) -> Result<Self, Error>;
57}