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
boolorOption(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(NOTIntoorTryInto) - Collection traits (C-COLLECT):
FromIterator,Extend(for collection types)
§5. Security (C-SEND-SYNC, C-GOOD-ERR)
- Types are
SendandSyncwhere 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
Zeroizefor automatic memory clearing
§6. Explicit Over Implicit (C-DEREF, C-CTOR)
- Only smart pointers implement
DerefandDerefMut(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_stdsupport 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 supportarbitrary- Enable arbitrary value generation for fuzzingzeroize- 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