Trait indexed_valued_enums::valued_enum::Valued
source · pub trait Valued: Indexed {
type Value;
const VALUES: &'static [Self::Value];
// Provided methods
fn value_opt(&self) -> Option<Self::Value> { ... }
fn value(&self) -> Self::Value { ... }
fn value_ref_opt(&self) -> Option<&'static Self::Value> { ... }
fn value_ref(&self) -> &'static Self::Value { ... }
fn value_to_variant_opt(value: &Self::Value) -> Option<Self>
where Self::Value: PartialEq { ... }
fn value_to_variant(value: &Self::Value) -> Self
where Self::Value: PartialEq { ... }
}
Expand description
Allows to get a value from an enum’s variant, where this enum implements Indexed, for example, having the following implementation:
use indexed_valued_enums::{indexed_enum::Indexed, valued_enum::Valued};
enum Number{ First, Second, Third }
impl Indexed for Number{
const VARIANTS: &'static [Self] = &[Number::First, Number::Second, Number::Third];
}
impl Valued for Number{
type Value = u16;
const VALUES: &'static [Self::Value] = &[100,200,300];
}
Calling Valued::value on every enum produces [First->100, Second->200, Third->300]
Since the type of the values (u16) implements PartialEq, we can also call Valued::value_to_variant to get the variants corresponding to the values [100->First, 200->Second, 300->Third]
Note this documentation it’s solely informational, it is dis-recommended to implement this trait manually, but using the derive macro crate::Valued or the declarative macro crate::create_indexed_valued_enum instead.
Required Associated Types§
Required Associated Constants§
sourceconst VALUES: &'static [Self::Value]
const VALUES: &'static [Self::Value]
Values each enumeration resolves to, each value must be stored to match it’s corresponding variant, this means it must be sorted in the same order as Indexed::VARIANTS
This means values must be const
Provided Methods§
sourcefn value_opt(&self) -> Option<Self::Value>
fn value_opt(&self) -> Option<Self::Value>
Gives the value corresponding to this variant, this is an O(1) operation as it just gets the value as a copy from Valued::VALUES
The type of Valued::Value doesn’t need to implement the Clone trait as the array is treated as a raw pointer whose value is read without cloning through core::ptr::read
If you just need a reference to the value, use Valued::value_ref_opt instead, as it doesn’t do a read copy.
Note that if implemented correctly (ensured by using crate::create_indexed_valued_enum), calling this method will always produce [Option::Some(Value)]
sourcefn value(&self) -> Self::Value
fn value(&self) -> Self::Value
Gives the value corresponding to this variant, this is an O(1) operation as it just gets the value as a copy from Valued::VALUES If you just need a reference to the value, use Valued::value_opt instead, as it doesn’t do a read copy.
sourcefn value_ref_opt(&self) -> Option<&'static Self::Value>
fn value_ref_opt(&self) -> Option<&'static Self::Value>
Gives the value corresponding for a variant of an enum marked with #[repr(usize)] and implementing the Valued trait, this is an O(1) operation as it just gets a reference to the value as a copy.
If you need the value as structure but it doesn’t implement clone, use value_opt_internal instead, as it performs a read copy
Note that if implemented correctly (ensured by the declarative macro crate::create_indexed_valued_enum), calling this method will always produce [Option::Some(&Value)]
sourcefn value_ref(&self) -> &'static Self::Value
fn value_ref(&self) -> &'static Self::Value
Gives the value corresponding for a variant of an enum marked with #[repr(usize)] and implementing the Valued trait, this is an O(1) operation as it just gets a reference to the value as a copy.
If you need the value as structure but it doesn’t implement clone, use value_internal instead, as it performs a read copy
Note that if implemented correctly (ensured by the declarative macro crate::create_indexed_valued_enum), calling this method will never panic
sourcefn value_to_variant_opt(value: &Self::Value) -> Option<Self>
fn value_to_variant_opt(value: &Self::Value) -> Option<Self>
Gives variant corresponding to a value, this is an O(n) operation as it does so by comparing every single value contained in Valued::VALUES
sourcefn value_to_variant(value: &Self::Value) -> Self
fn value_to_variant(value: &Self::Value) -> Self
Gives variant corresponding to a value, this is an O(n) operation as it does so by comparing every single value contained in Valued::VALUES