Skip to main content

Crate bare_types

Crate bare_types 

Source
Expand description

§bare-types

A zero-cost foundation for type-safe domain modeling in Rust.

§Overview

bare-types provides strongly-typed, zero-cost abstractions for domain modeling in Rust. It implements the Rust API Guidelines and follows strict design principles to ensure type safety, performance, and correctness.

§Design Philosophy

§Parse, Don’t Validate

This project follows the “Parse, don’t validate” philosophy. Instead of validating data throughout your codebase, parse it once at the system boundary and use strong types to ensure invariants are maintained.

This approach provides:

  • Type safety: Invalid states are unrepresentable
  • Zero-cost abstractions: Validation happens once at construction
  • Clear error handling: Errors are caught early and explicitly
  • Self-documenting code: Types convey meaning

§Design Rules

This project adheres to the Rust API Guidelines and implements the following design rules:

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

  • Use newtype pattern to provide static distinctions between types (C-NEWTYPE)
  • Arguments convey meaning through types, not bool or Option (C-CUSTOM-TYPE)
  • All validation is performed at construction time
  • Invalid states are unrepresentable

§2. Zero-Cost Abstractions

  • All validation is performed at compile time or construction time
  • No runtime cost for accessing validated data
  • Use #[repr(transparent)] for newtypes over primitive types
  • Memory layout matches underlying types

§3. RFC Compliance

Strictly follow relevant standards:

  • 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 for easy composition:

  • Common traits (C-COMMON-TRAITS): Debug, Clone, Display, Hash, Eq, PartialEq, Ord, PartialOrd, Default
  • Conversion traits (C-CONV-TRAITS): From, TryFrom, AsRef, AsMut (NOT Into or TryInto)
  • Collection traits (C-COLLECT): FromIterator, Extend (for collection types)

§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 (unsafe_code = "forbid")
  • 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 code over implicit behavior
  • No declarative macros except for serde derives

§7. Strict Linting

The project enforces strict linting rules:

  • unsafe_code = "forbid"
  • missing_docs = "warn"
  • Deny-level clippy rules for safety and correctness

§Design Goals

§Performance

  • Zero-cost abstractions for type safety
  • Efficient memory usage with optimized data structures
  • No runtime overhead for validated types

§Portability

  • Cross-platform: Linux, macOS, Windows
  • no_std support for embedded and WASM
  • Minimal dependencies for reduced attack surface

§Ergonomics

  • Type-safe APIs prevent common errors
  • Clear error messages with detailed context
  • Comprehensive documentation with examples
  • Follow Rust naming conventions (RFC 430)

§Security

  • Memory safety guaranteed by Rust
  • Type safety prevents many classes of bugs
  • No unsafe code in codebase
  • Minimal external dependencies

§Features

  • std - Enable standard library support (default)
  • net - Network-related types (IP addresses, ports, hostnames, etc.)
  • serde - Enable serde serialization/deserialization support
  • arbitrary - Enable arbitrary value generation for fuzzing
  • zeroize - Enable automatic memory clearing for sensitive data

§Modules

§net (requires net feature)

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

§License

MIT OR Apache-2.0

Modules§

netnet
Network-related types for building type-safe network applications.
preludestd
Prelude module for convenient imports.
syssys
System information types for building type-safe system-level applications.