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§
Required Methods§
Sourcefn new<A>() -> <Self as Once>::Of<A>
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::classes::once::Once;
use fp_library::brands::OnceCellBrand;
let cell = <OnceCellBrand as Once>::new::<i32>();
assert_eq!(<OnceCellBrand as Once>::get(&cell), None);Sourcefn get<A>(a: &<Self as Once>::Of<A>) -> Option<&A>
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::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));Sourcefn get_mut<A>(a: &mut <Self as Once>::Of<A>) -> Option<&mut A>
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::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));Sourcefn set<A>(a: &<Self as Once>::Of<A>, value: A) -> Result<(), A>
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::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());Sourcefn get_or_init<A, B: FnOnce() -> A>(a: &<Self as Once>::Of<A>, f: B) -> &A
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::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);Sourcefn into_inner<A>(a: <Self as Once>::Of<A>) -> Option<A>
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::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));Sourcefn take<A>(a: &mut <Self as Once>::Of<A>) -> Option<A>
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::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.