Expand description
This crate provides a set of traits for standardizing object creation patterns in Rust, covering infallible, fallible, conditional, and policy-based validated construction.
§Core Traits
IntoInner: A trait for types that can be converted into an “inner” value. This is often a prerequisite for construction traits.TryNew: For fallible construction that returns aResult. Use this when creating an instance might fail due to validation or other reasons.New: For infallible construction. If creation cannot logically fail (or if failure should deterministically panic), use this trait.
§Advanced Construction Patterns
ConditionallyCreate: Provides acreate_conditionallymethod that behaves differently in debug and release builds. In debug, it usesTryNew::try_new().expect(), panicking on failure. In release, it usesNew::new(). This is useful for enforcing stricter checks during development.ValidationPolicy: Defines a contract for validation logic. A policy specifies how a value should be validated and what error type is returned upon failure. This allows for reusable validation strategies.TryNewValidated: ExtendsTryNewby associating a specificValidationPolicywith the type. TheTryNewValidated::try_new_validatedmethod first applies the policy and then, if successful, proceeds with the underlyingTryNewconstruction logic. This enables a two-phase construction process: external validation followed by internal creation.
§Features
std: (Enabled by default) When enabled, theErrorassociated type forTryNewandValidationPolicyis bound bystd::error::Error.- If
stdis not enabled, theirErrortypes are bound bycore::fmt::Debug.
§Examples
See the documentation for individual traits for specific examples.
Traits§
- Conditionally
Create - A trait for conditionally creating objects.
- Into
Inner - A trait for consuming a wrapper type and extracting its inner value.
- New
- A trait for creating new instances of a type infallibly.
- TryNew
- A trait for creating new instances of a type with fallible validation.
- TryNew
Validated - A trait for creating new instances of a type with fallible validation,
where the validation logic is defined by an associated
ValidationPolicy. - Validation
Policy - Defines a contract for validation policies.
Derive Macros§
- Into
Inner - A derive macro for automatically implementing the
IntoInnertrait for tuple structs.