Skip to main content

TryNew

Trait TryNew 

Source
pub trait TryNew: Sized + IntoInner {
    type Error: Error;

    // Required method
    fn try_new(value: Self::InnerType) -> Result<Self, Self::Error>;
}
Expand description

A trait for creating new instances of a type with fallible validation.

This trait provides a standardized way to create new instances of a type from an “inner” value, where the construction process might fail (e.g., due to validation rules). Implementors must also implement IntoInner, which defines the InnerType used for construction.

§Associated Types

  • InnerType: (From the IntoInner supertrait) The type of the input value used to attempt creation of a new instance of Self.
  • Error: The error type that is returned if try_new fails. This error type must implement std::error::Error if the std feature is enabled. If the std feature is not enabled, it must implement core::fmt::Debug.

§Examples

use try_create::{TryNew, IntoInner};

// Define a custom error type
#[derive(Debug, PartialEq)]
struct NotPositiveError;

// Implement Display and Error for the custom error (required if using std::error::Error)
impl fmt::Display for NotPositiveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Value must be positive")
    }
}
impl Error for NotPositiveError {}


// A struct that wraps an i32, ensuring it's positive.
#[derive(Debug, PartialEq)]
struct PositiveInteger {
    value: i32,
}

// Implement IntoInner to define what `InnerType` is.
impl IntoInner for PositiveInteger {
    type InnerType = i32;

    fn into_inner(self) -> Self::InnerType {
        self.value
    }
}

// Implement TryNew for PositiveInteger.
impl TryNew for PositiveInteger {
    type Error = NotPositiveError;
    // `InnerType` is `i32`, inherited from `IntoInner`.

    fn try_new(value: Self::InnerType) -> Result<Self, Self::Error> {
        if value > 0 {
            Ok(PositiveInteger { value })
        } else {
            Err(NotPositiveError)
        }
    }
}

// Usage
assert_eq!(PositiveInteger::try_new(10), Ok(PositiveInteger { value: 10 }));
assert_eq!(PositiveInteger::try_new(0), Err(NotPositiveError));
assert_eq!(PositiveInteger::try_new(-5), Err(NotPositiveError));

let positive_num = PositiveInteger::try_new(42).unwrap();
assert_eq!(positive_num.into_inner(), 42);

Required Associated Types§

Source

type Error: Error

The error type that can be returned by the try_new method.

Required Methods§

Source

fn try_new(value: Self::InnerType) -> Result<Self, Self::Error>

Attempts to create a new instance of Self from value.

§Parameters
  • value: The InnerType value from which to create the instance.
§Returns
  • Ok(Self) if the instance is created successfully.
  • Err(Self::Error) if creation fails (e.g., due to invalid input).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl TryNew for Complex<f32>

Source§

fn try_new( value: <Complex<f32> as IntoInner>::InnerType, ) -> Result<Complex<f32>, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for Complex<f64>

Source§

fn try_new( value: <Complex<f64> as IntoInner>::InnerType, ) -> Result<Complex<f64>, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for bool

Source§

fn try_new(value: <bool as IntoInner>::InnerType) -> Result<bool, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for char

Source§

fn try_new(value: <char as IntoInner>::InnerType) -> Result<char, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for f32

Source§

fn try_new(value: <f32 as IntoInner>::InnerType) -> Result<f32, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for f64

Source§

fn try_new(value: <f64 as IntoInner>::InnerType) -> Result<f64, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for i8

Source§

fn try_new(value: <i8 as IntoInner>::InnerType) -> Result<i8, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for i16

Source§

fn try_new(value: <i16 as IntoInner>::InnerType) -> Result<i16, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for i32

Source§

fn try_new(value: <i32 as IntoInner>::InnerType) -> Result<i32, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for i64

Source§

fn try_new(value: <i64 as IntoInner>::InnerType) -> Result<i64, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for i128

Source§

fn try_new(value: <i128 as IntoInner>::InnerType) -> Result<i128, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for isize

Source§

fn try_new(value: <isize as IntoInner>::InnerType) -> Result<isize, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for u8

Source§

fn try_new(value: <u8 as IntoInner>::InnerType) -> Result<u8, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for u16

Source§

fn try_new(value: <u16 as IntoInner>::InnerType) -> Result<u16, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for u32

Source§

fn try_new(value: <u32 as IntoInner>::InnerType) -> Result<u32, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for u64

Source§

fn try_new(value: <u64 as IntoInner>::InnerType) -> Result<u64, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for u128

Source§

fn try_new(value: <u128 as IntoInner>::InnerType) -> Result<u128, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Source§

impl TryNew for usize

Source§

fn try_new(value: <usize as IntoInner>::InnerType) -> Result<usize, !>

Creates a new instance of the fundamental type from the given value.

This implementation is infallible and simply returns the input value.

Source§

type Error = !

Implementors§