ipnet/lib.rs
1#![doc(html_root_url = "https://docs.rs/ipnet/2.12.0")]
2//! Types for IPv4 and IPv6 network addresses.
3//!
4//! This module provides types and useful methods for working with IPv4
5//! and IPv6 network addresses, commonly called IP prefixes. The new
6//! [`IpNet`], [`Ipv4Net`], and [`Ipv6Net`] types build on the existing
7//! [`IpAddr`], [`Ipv4Addr`], and [`Ipv6Addr`] types already provided in
8//! Rust's standard library and align to their design to stay
9//! consistent.
10//!
11//! The module also provides the [`IpSubnets`], [`Ipv4Subnets`], and
12//! [`Ipv6Subnets`] types for iterating over the subnets contained in
13//! an IP address range. The [`IpAddrRange`], [`Ipv4AddrRange`], and
14//! [`Ipv6AddrRange`] types for iterating over IP addresses in a range.
15//! And traits that extend `Ipv4Addr` and `Ipv6Addr` with methods for
16//! addition, subtraction, bitwise-and, and bitwise-or operations that
17//! are missing in Rust's standard library.
18//!
19//! The module only uses stable features so it is guaranteed to compile
20//! using the stable toolchain.
21//!
22//! # Organization
23//!
24//! * [`IpNet`] represents an IP network address, either IPv4 or IPv6.
25//! * [`Ipv4Net`] and [`Ipv6Net`] are respectively IPv4 and IPv6 network
26//! addresses.
27//! * [`IpSubnets`], [`Ipv4Subnets`], and [`Ipv6Subnets`] are iterators
28//! that generate the smallest set of IP network addresses bound by an
29//! IP address range and minimum prefix length. These can be created
30//! using their constructors. They are also returned by the
31//! [`subnets()`] methods and used within the [`aggregate()`] methods.
32//! * [`IpAddrRange`], [`Ipv4AddrRange`], and [`Ipv6AddrRange`] are
33//! iterators that generate IP addresses. These can be created using
34//! their constructors. They are also returned by the [`hosts()`]
35//! methods.
36//! * The [`IpAdd`], [`IpSub`], [`IpBitAnd`], [`IpBitOr`] traits extend
37//! the [`Ipv4Addr`] and [`Ipv6Addr`] types with methods to perform
38//! these operations.
39//!
40//! [`IpNet`]: enum.IpNet.html
41//! [`Ipv4Net`]: struct.Ipv4Net.html
42//! [`Ipv6Net`]: struct.Ipv6Net.html
43//! [`IpAddr`]: https://doc.rust-lang.org/std/net/enum.IpAddr.html
44//! [`Ipv4Addr`]: https://doc.rust-lang.org/std/net/struct.Ipv4Addr.html
45//! [`Ipv6Addr`]: https://doc.rust-lang.org/std/net/struct.Ipv6Addr.html
46//! [`IpSubnets`]: enum.IpSubnets.html
47//! [`Ipv4Subnets`]: struct.Ipv4Subnets.html
48//! [`Ipv6Subnets`]: struct.Ipv6Subnets.html
49//! [`subnets()`]: enum.IpNet.html#method.subnets
50//! [`aggregate()`]: enum.IpNet.html#method.aggregate
51//! [`IpAddrRange`]: enum.IpAddrRange.html
52//! [`Ipv4AddrRange`]: struct.Ipv4AddrRange.html
53//! [`Ipv6AddrRange`]: struct.Ipv6AddrRange.html
54//! [`hosts()`]: enum.IpNet.html#method.hosts
55//! [`IpAdd`]: trait.IpAdd.html
56//! [`IpSub`]: trait.IpSub.html
57//! [`IpBitAnd`]: trait.IpBitAnd.html
58//! [`IpBitOr`]: trait.IpBitOr.html
59//!
60//! # Features
61//!
62//! These flags can be used to extend functionality using third-party
63//! dependencies or optional libraries. See the [features] reference
64//! for more information.
65//!
66//! [features]: https://doc.rust-lang.org/cargo/reference/features.html#the-features-section
67//!
68//! ## "std"
69//!
70//! Enabled by default. Disabling this feature will mandate the use of the
71//! [core] and [alloc] crates where applicable instead of [std].
72//!
73//! [core]: https://doc.rust-lang.org/core/
74//! [alloc]: https://doc.rust-lang.org/alloc/
75//! [std]: https://doc.rust-lang.org/std/
76//!
77//! ## "serde"
78//!
79//! Uses [`serde`] to implement the `Serialize` and
80//! `Deserialize` traits.
81//!
82//! For human readable formats (e.g. JSON) the `IpNet`, `Ipv4Net`, and
83//! `Ipv6Net` types will serialize to their `Display` strings.
84//!
85//! For compact binary formats (e.g. Bincode) the `Ipv4Net` and
86//! `Ipv6Net` types will serialize to a string of 5 and 17 bytes that
87//! consist of the network address octects followed by the prefix
88//! length. The `IpNet` type will serialize to an Enum with the V4 or V6
89//! variant index prepending the above string of 5 or 17 bytes.
90//!
91//! [`serde`]: https://serde.rs
92//!
93//! ## "heapless" [^1]
94//!
95//! Uses [`heapless`] to optimize serialization performance by avoiding
96//! dynamic allocations.
97//!
98//! [`heapless`]: https://docs.rs/heapless/latest/heapless/
99//!
100//! ## "schemars08"
101//!
102//! Uses [`schemars@0.8.*`] to implement the `JsonSchema` trait.
103//!
104//! [`schemars@0.8.*`]: https://docs.rs/schemars/0.8/schemars/
105//!
106//! ## "schemars1"
107//!
108//! Uses [`schemars@1.*`] to implement the `JsonSchema` trait.
109//!
110//! [`schemars@1.*`]: https://docs.rs/schemars/1/schemars/
111//!
112//! ## Legacy features
113//!
114//! The following features are set to be removed in the next major
115//! release. Use the provided analogs instead.
116//!
117//! | Name | Analog | Reason for removal |
118//! | ------------ | ------------------------ | --------------------------------------------------------------- |
119//! | `json` [^1] | `schemars08` and `serde` | Unconventional naming. |
120//! | `ser_as_str` | `heapless` [^1] | Doesn't enable the `serde` feature but does nothing on its own. |
121//! | `schemars` | `schemars08` | Replaced by `schemars08`. |
122//!
123//! [^1]: Enabling these features will also enable the `serde` feature.
124//!
125
126#![no_std]
127
128#[cfg(feature = "std")]
129extern crate std;
130
131#[cfg_attr(test, macro_use)]
132extern crate alloc;
133
134#[cfg(feature = "schemars08")]
135extern crate schemars08;
136#[cfg(feature = "schemars1")]
137extern crate schemars1;
138#[cfg(feature = "serde")]
139extern crate serde;
140
141pub use self::ipext::{IpAdd, IpAddrRange, IpBitAnd, IpBitOr, IpSub, Ipv4AddrRange, Ipv6AddrRange};
142pub use self::ipnet::{
143 IpNet, IpSubnets, Ipv4Net, Ipv4Subnets, Ipv6Net, Ipv6Subnets, PrefixLenError,
144};
145pub use self::mask::{ip_mask_to_prefix, ipv4_mask_to_prefix, ipv6_mask_to_prefix};
146pub use self::parser::AddrParseError;
147
148mod ipext;
149mod ipnet;
150mod mask;
151mod parser;
152
153#[cfg(feature = "schemars08")]
154mod ipnet_schemars_08;
155#[cfg(feature = "schemars1")]
156mod ipnet_schemars_1;
157#[cfg(feature = "serde")]
158mod ipnet_serde;