Once

Trait Once 

Source
pub trait Once: Kind_ad6c20556a82a1f0 {
    type Of<A>;

    // Required methods
    fn new<A>() -> <Self as Once>::Of<A>;
    fn get<A>(a: &<Self as Once>::Of<A>) -> Option<&A>;
    fn get_mut<A>(a: &mut <Self as Once>::Of<A>) -> Option<&mut A>;
    fn set<A>(a: &<Self as Once>::Of<A>, value: A) -> Result<(), A>;
    fn get_or_init<A, B: FnOnce() -> A>(a: &<Self as Once>::Of<A>, f: B) -> &A;
    fn into_inner<A>(a: <Self as Once>::Of<A>) -> Option<A>;
    fn take<A>(a: &mut <Self as Once>::Of<A>) -> Option<A>;
}
Expand description

A type class for containers that hold a value that is initialized at most once.

It provides methods for initialization, access, and consumption.

Required Associated Types§

Source

type Of<A>

Required Methods§

Source

fn new<A>() -> <Self as Once>::Of<A>

Creates a new, uninitialized Once container.

This method creates a new instance of the container that is initially empty.

§Type Signature

forall a. Once f => () -> f a

§Type Parameters
  • A: The type of the value to be stored in the container.
§Returns

A new, empty container.

§Examples
use fp_library::{brands::*, functions::*};

let cell = once_new::<OnceCellBrand, i32>();
assert_eq!(once_get::<OnceCellBrand, _>(&cell), None);
Source

fn get<A>(a: &<Self as Once>::Of<A>) -> Option<&A>

Gets a reference to the value if it has been initialized.

This method returns a reference to the value stored in the container if it has been initialized, otherwise it returns None.

§Type Signature

forall a. Once f => f a -> Option a

§Type Parameters
  • A: The type of the value stored in the container.
§Parameters
  • a: The container.
§Returns

A reference to the value, or None if uninitialized.

§Examples
use fp_library::{brands::*, functions::*};

let cell = once_new::<OnceCellBrand, i32>();
assert_eq!(once_get::<OnceCellBrand, _>(&cell), None);
once_set::<OnceCellBrand, _>(&cell, 42).unwrap();
assert_eq!(once_get::<OnceCellBrand, _>(&cell), Some(&42));
Source

fn get_mut<A>(a: &mut <Self as Once>::Of<A>) -> Option<&mut A>

Gets a mutable reference to the value if it has been initialized.

This method returns a mutable reference to the value stored in the container if it has been initialized, otherwise it returns None.

§Type Signature

forall a. Once f => f a -> Option a

§Type Parameters
  • A: The type of the value stored in the container.
§Parameters
  • a: The container.
§Returns

A mutable reference to the value, or None if uninitialized.

§Examples
use fp_library::{brands::*, functions::*};

let mut cell = once_new::<OnceCellBrand, i32>();
once_set::<OnceCellBrand, _>(&cell, 42).unwrap();
if let Some(val) = once_get_mut::<OnceCellBrand, _>(&mut cell) {
    *val += 1;
}
assert_eq!(once_get_mut::<OnceCellBrand, _>(&mut cell), Some(&mut 43));
Source

fn set<A>(a: &<Self as Once>::Of<A>, value: A) -> Result<(), A>

Sets the value of the container.

This method attempts to set the value of the container. If the container is already initialized, it returns the value in the Err variant.

§Type Signature

forall a. Once f => (f a, a) -> Result<(), a>

§Type Parameters
  • A: The type of the value to set.
§Parameters
  • a: The container.
  • value: The value to set.
§Returns

Ok(()) on success, or Err(value) if already initialized.

§Examples
use fp_library::{brands::*, functions::*};

let cell = once_new::<OnceCellBrand, i32>();
assert!(once_set::<OnceCellBrand, _>(&cell, 42).is_ok());
assert!(once_set::<OnceCellBrand, _>(&cell, 10).is_err());
Source

fn get_or_init<A, B: FnOnce() -> A>(a: &<Self as Once>::Of<A>, f: B) -> &A

Gets the value, initializing it with the closure f if it is not already initialized.

This method returns a reference to the value stored in the container. If the container is not initialized, it initializes it using the provided closure f and then returns a reference to the value.

§Type Signature

forall a. Once f => (f a, () -> a) -> a

§Type Parameters
  • A: The type of the value stored in the container.
  • B: The type of the initialization function.
§Parameters
  • a: The container.
  • f: The initialization function.
§Returns

A reference to the value.

§Examples
use fp_library::{brands::*, functions::*};

let cell = once_new::<OnceCellBrand, i32>();
assert_eq!(*once_get_or_init::<OnceCellBrand, _, _>(&cell, || 42), 42);
assert_eq!(*once_get_or_init::<OnceCellBrand, _, _>(&cell, || 10), 42);
Source

fn into_inner<A>(a: <Self as Once>::Of<A>) -> Option<A>

Consumes the container and returns the value if it has been initialized.

This method consumes the container and returns the value stored in it if it has been initialized, otherwise it returns None.

§Type Signature

forall a. Once f => f a -> Option a

§Type Parameters
  • A: The type of the value stored in the container.
§Parameters
  • a: The container.
§Returns

The value, or None if uninitialized.

§Examples
use fp_library::{brands::*, functions::*};

let cell = once_new::<OnceCellBrand, i32>();
once_set::<OnceCellBrand, _>(&cell, 42).unwrap();
assert_eq!(once_into_inner::<OnceCellBrand, _>(cell), Some(42));
Source

fn take<A>(a: &mut <Self as Once>::Of<A>) -> Option<A>

Takes the value out of the container, leaving it uninitialized.

This method takes the value out of the container, leaving the container in an uninitialized state. It returns the value if it was initialized, otherwise it returns None.

§Type Signature

forall a. Once f => f a -> Option a

§Type Parameters
  • A: The type of the value stored in the container.
§Parameters
  • a: The container.
§Returns

The value, or None if uninitialized.

§Examples
use fp_library::{brands::*, functions::*};

let mut cell = once_new::<OnceCellBrand, i32>();
once_set::<OnceCellBrand, _>(&cell, 42).unwrap();
assert_eq!(once_take::<OnceCellBrand, _>(&mut cell), Some(42));
assert_eq!(once_take::<OnceCellBrand, _>(&mut cell), None);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§