Crate try_create

Crate try_create 

Source
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 a Result. 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 a create_conditionally method that behaves differently in debug and release builds. In debug, it uses TryNew::try_new().expect(), panicking on failure. In release, it uses New::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: Extends TryNew by associating a specific ValidationPolicy with the type. The TryNewValidated::try_new_validated method first applies the policy and then, if successful, proceeds with the underlying TryNew construction logic. This enables a two-phase construction process: external validation followed by internal creation.

§Features

  • std: (Enabled by default) When enabled, the Error associated type for TryNew and ValidationPolicy is bound by std::error::Error.
  • If std is not enabled, their Error types are bound by core::fmt::Debug.

§Examples

See the documentation for individual traits for specific examples.

Traits§

ConditionallyCreate
A trait for conditionally creating objects.
IntoInner
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.
TryNewValidated
A trait for creating new instances of a type with fallible validation, where the validation logic is defined by an associated ValidationPolicy.
ValidationPolicy
Defines a contract for validation policies.

Derive Macros§

IntoInner
A derive macro for automatically implementing the IntoInner trait for tuple structs.