cidr
Rust types for representing IP networks (CIDR) and hosts within a network (Inet)
Repository Health
Technical Analysis
cidr is a Rust library providing data types to represent IP networks and individual hosts within them. Its Cidr type models an IP network prefix (an address plus a netmask length), while its Inet type models a specific host address together with the network it belongs to, mirroring the semantics of PostgreSQL’s cidr and inet column types.
The crate supports both IPv4 and IPv6 through concrete and any-family variants, offers parsing and display, iteration over the hosts in a network, and optional serde and bitstring integrations. It is a small, focused building block for network tooling, address management, and anything that needs to reason about IP ranges.
What You Get
- Cidr types for IP network prefixes and Inet types for a host within a network
- Separate IPv4/IPv6 types plus family-agnostic AnyIpCidr / IpInet variants
- Parsing from strings, Display formatting, and containment/overlap checks
- Iteration over addresses in a network and optional serde and bitstring features
Common Use Cases
- Parsing and validating CIDR notation in network configuration or CLIs
- Checking whether an IP address falls within a given network range
- Enumerating hosts in a subnet for scanning or allocation tools
Under The Hood
Architecture - The crate is organized around a set of parallel types: address and family helpers (address.rs, family.rs, num.rs), the network-prefix types under cidr/, the host-in-network types under inet/, plus inet_pair/ and inet_iterator.rs for ranges and iteration. Shared behavior is expressed through traits in traits.rs and internal_traits.rs, so IPv4, IPv6, and the any-family enums (AnyIpCidr, IpInet) implement a common interface. Parsing lives in parsers/ and error types in errors.rs, with serde wiring isolated in serde_common.rs.
Tech Stack - Pure Rust (edition 2021) building on std’s IpAddr/Ipv4Addr/Ipv6Addr. Optional feature flags enable serde serialization and bitstring trait support (used with the bitstring-trees crate for prefix maps). No heavy runtime dependencies.
Code Quality - Spread across ~31 source files with clear per-concern modules, the code uses traits to avoid duplication across families and includes version-sync checks in CI. The trait-heavy design keeps IPv4/IPv6 behavior consistent.
API Design - Naming deliberately follows PostgreSQL’s cidr/inet types, which makes the distinction between a network and a host-in-network intuitive to database-aware users. Values parse via FromStr and format via Display, and containment/iteration read naturally; the main learning curve is choosing among the concrete versus any-family types for a given use case.