pub trait OptionExt<T>: Sealedwhere
T: Assign<T, T>,{
// Required method
fn option_assign(&mut self, src: T);
}
Expand description
Extension trait to provide a method for setting a component on an Option<Self>
if the Option
is currently None
. If the Option
is Some
, the method will
delegate to the Assign
trait’s assign
method.
§Type Parameters
T
: The type of the component to be set on the implementing object. This type represents the final form of the component as it should be stored or represented in the object.
§Examples
Using option_assign
to set a component on an Option
:
use former_types::{ Assign, OptionExt }; // use crate `former` instead of crate `former_types` unless you need to use crate `former_types` directly
struct MyStruct
{
name : String,
}
impl< IntoT : Into< MyStruct > > Assign< MyStruct, IntoT > for MyStruct
{
fn assign( &mut self, component : IntoT )
{
self.name = component.into().name;
}
}
let mut opt_struct: Option< MyStruct > = None;
opt_struct.option_assign( MyStruct { name: "New Name".to_string() } );
assert_eq!( opt_struct.unwrap().name, "New Name" );
Required Methods§
Sourcefn option_assign(&mut self, src: T)
fn option_assign(&mut self, src: T)
Sets the component on the Option
if it is None
.
If the Option
is Some
, the assign
method is called to update the existing value.
§Parameters
src
: The value to assign to theOption
.