Skip to main content

Module net

Module net 

Source
Available on crate feature net only.
Expand description

Network-related types for building type-safe network applications.

This module provides strongly-typed abstractions for common network concepts, ensuring type safety and preventing common errors in network programming.

§Design Rules

All types in this module follow the core design rules, which adhere to Rust API Guidelines:

§1. Type Safety (C-NEWTYPE, C-CUSTOM-TYPE)

  • Strong types prevent mixing incompatible values
  • Arguments convey meaning through types, not bool or Option
  • All validation performed at construction time
  • No runtime errors from invalid states
  • Invalid states are unrepresentable

§2. Zero-Cost Abstractions

  • No runtime overhead compared to using primitive types
  • All validation happens at compile time or construction
  • Subsequent operations have zero cost
  • Use #[repr(transparent)] for newtypes over primitive types

§3. RFC Compliance

  • Domain names: RFC 1035
  • Hostnames: RFC 1123
  • IP addresses: Standard IPv4/IPv6

§4. Composability (C-COMMON-TRAITS, C-CONV-TRAITS)

All types implement standard traits:

  • Common traits (C-COMMON-TRAITS):
    • Debug for debugging
    • Clone for copying
    • Display for formatting
    • Hash for use in hash maps
    • Eq and PartialEq for equality
    • Ord and PartialOrd for ordering (where applicable)
    • Default for default values (where applicable)
  • Conversion traits (C-CONV-TRAITS):
    • From for infallible conversions
    • TryFrom for fallible conversions
    • AsRef for cheap borrowed conversions
    • AsMut for cheap borrowed mutable conversions
    • NOT Into or TryInto (these are blanket impls)

§5. Security (C-SEND-SYNC, C-GOOD-ERR)

  • Types are Send and Sync where possible (C-SEND-SYNC)
  • Error types implement std::error::Error, Send, Sync (C-GOOD-ERR)
  • No unsafe code allowed
  • Sensitive data uses Zeroize for automatic memory clearing

§6. Explicit Over Implicit (C-DEREF, C-CTOR)

  • Only smart pointers implement Deref and DerefMut (C-DEREF)
  • Constructors are static, inherent methods (C-CTOR)
  • Prefer explicit constructors
  • Clear, readable code over clever abstractions
  • No declarative macros except for serde derives

§Types

This module includes types for:

  • IP addresses (IPv4 and IPv6)
  • Port numbers with validation
  • Domain names with RFC 1035 validation
  • Hostnames with RFC 1123 validation
  • Host type that can represent IP addresses, domain names, or hostnames
  • Socket addresses combining hosts and ports

§Features

This module requires net feature to be enabled.

[dependencies]
bare-types = { version = "0.1", features = ["net"] }

§no_std Support

This module is fully no_std compatible. All types are built on core::net (available in Rust 1.82+) and do not require the standard library.

To use in a no_std environment:

[dependencies]
bare-types = { version = "0.1", default-features = false, features = ["net"] }

The std feature can be optionally enabled for std::error::Error implementations.

Re-exports§

pub use cidr::Cidr;
pub use cidr::CidrError;
pub use domainname::DomainName;
pub use domainname::DomainNameError;
pub use email::Email;
pub use email::EmailError;
pub use host::Host;
pub use host::HostError;
pub use hostname::Hostname;
pub use hostname::HostnameError;
pub use ipaddr::IpAddr;
pub use ipaddr::IpAddrError;
pub use macaddress::MacAddress;
pub use macaddress::MacAddressError;
pub use port::Port;
pub use port::PortError;
pub use socketaddr::SocketAddr;
pub use socketaddr::SocketAddrError;
pub use socketaddr::StdConversionError;
pub use url::Url;
pub use url::UrlError;
pub use uuid::Uuid;
pub use uuid::UuidError;
pub use uuid::UuidVariant;

Modules§

cidr
CIDR (Classless Inter-Domain Routing) notation for IP networks.
domainname
Domain name type for DNS programming.
email
Email address type for network programming.
host
Host type for network programming.
hostname
Hostname type for network programming.
ipaddr
IP address type for network programming.
macaddress
MAC address type for network programming.
port
Port number type for network programming.
socketaddr
Socket address type for network programming.
url
URL type for network programming.
uuid
UUID type for network programming.