Once

Trait Once 

Source
pub trait Once: Kind_bd4ddc17b95f4bc6 {
    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 types that can be initialized once.

Once represents a container that holds 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.

§Type Signature

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

§Returns

A new, empty container.

§Examples
use fp_library::classes::once::Once;
use fp_library::brands::OnceCellBrand;

let cell = <OnceCellBrand as Once>::new::<i32>();
assert_eq!(<OnceCellBrand as Once>::get(&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.

§Type Signature

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

§Parameters
  • a: The container.
§Returns

A reference to the value, or None if uninitialized.

§Examples
use fp_library::classes::once::Once;
use fp_library::brands::OnceCellBrand;

let cell = <OnceCellBrand as Once>::new::<i32>();
assert_eq!(<OnceCellBrand as Once>::get(&cell), None);
<OnceCellBrand as Once>::set(&cell, 42).unwrap();
assert_eq!(<OnceCellBrand as Once>::get(&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.

§Type Signature

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

§Parameters
  • a: The container.
§Returns

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

§Examples
use fp_library::classes::once::Once;
use fp_library::brands::OnceCellBrand;

let mut cell = <OnceCellBrand as Once>::new::<i32>();
<OnceCellBrand as Once>::set(&cell, 42).unwrap();
if let Some(val) = <OnceCellBrand as Once>::get_mut(&mut cell) {
    *val += 1;
}
assert_eq!(<OnceCellBrand as Once>::get_mut(&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.

Returns Ok(()) if the value was set, or Err(value) if the container was already initialized.

§Type Signature

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

§Parameters
  • a: The container.
  • value: The value to set.
§Returns

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

§Examples
use fp_library::classes::once::Once;
use fp_library::brands::OnceCellBrand;

let cell = <OnceCellBrand as Once>::new::<i32>();
assert!(<OnceCellBrand as Once>::set(&cell, 42).is_ok());
assert!(<OnceCellBrand as Once>::set(&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.

§Type Signature

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

§Parameters
  • a: The container.
  • f: The initialization function.
§Returns

A reference to the value.

§Examples
use fp_library::classes::once::Once;
use fp_library::brands::OnceCellBrand;

let cell = <OnceCellBrand as Once>::new::<i32>();
assert_eq!(*<OnceCellBrand as Once>::get_or_init(&cell, || 42), 42);
assert_eq!(*<OnceCellBrand as Once>::get_or_init(&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.

§Type Signature

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

§Parameters
  • a: The container.
§Returns

The value, or None if uninitialized.

§Examples
use fp_library::classes::once::Once;
use fp_library::brands::OnceCellBrand;

let cell = <OnceCellBrand as Once>::new::<i32>();
<OnceCellBrand as Once>::set(&cell, 42).unwrap();
assert_eq!(<OnceCellBrand as Once>::into_inner(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.

§Type Signature

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

§Parameters
  • a: The container.
§Returns

The value, or None if uninitialized.

§Examples
use fp_library::classes::once::Once;
use fp_library::brands::OnceCellBrand;

let mut cell = <OnceCellBrand as Once>::new::<i32>();
<OnceCellBrand as Once>::set(&cell, 42).unwrap();
assert_eq!(<OnceCellBrand as Once>::take(&mut cell), Some(42));
assert_eq!(<OnceCellBrand as Once>::take(&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§