coremem_types: Enum: add constructors
This commit is contained in:
@@ -128,6 +128,26 @@ where
|
|||||||
#[derive(Copy, Clone, Default, PartialEq)]
|
#[derive(Copy, Clone, Default, PartialEq)]
|
||||||
struct Enum<D, L>(D, L);
|
struct Enum<D, L>(D, L);
|
||||||
|
|
||||||
|
impl<P: Peano, L> Enum<(Discr<P>,), L> {
|
||||||
|
pub fn new<Variants>(v: Variants) -> Self
|
||||||
|
where
|
||||||
|
Variants: IntoList<List=L>,
|
||||||
|
L: list::Meta<Length=P>,
|
||||||
|
{
|
||||||
|
Enum((Discr::default(),), v.into_list())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<L> Enum<(), L> {
|
||||||
|
pub fn internally_discriminated<Variants>(v: Variants) -> Self
|
||||||
|
where
|
||||||
|
Variants: IntoList<List=L>,
|
||||||
|
L: list::Meta + Indexable<P0>,
|
||||||
|
list::ElementAt<P0, L>: DiscriminantCodable<<L as list::Meta>::Length>,
|
||||||
|
{
|
||||||
|
Enum((), v.into_list())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
trait EnumRequirements {
|
trait EnumRequirements {
|
||||||
type NumVariants: Peano;
|
type NumVariants: Peano;
|
||||||
fn decode_discr(&self) -> Discr<Self::NumVariants>;
|
fn decode_discr(&self) -> Discr<Self::NumVariants>;
|
||||||
@@ -300,4 +320,13 @@ mod test {
|
|||||||
e.set::<P0>(BoxedF32(0f32));
|
e.set::<P0>(BoxedF32(0f32));
|
||||||
assert_eq!(e.dispatch(ReadReceiver), 0);
|
assert_eq!(e.dispatch(ReadReceiver), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn new() {
|
||||||
|
let e = Enum::new((5u32, 4i32));
|
||||||
|
assert_eq!(e.dispatch(ReadReceiver), 5);
|
||||||
|
|
||||||
|
let e = Enum::internally_discriminated((BoxedF32(4f32), -1i32));
|
||||||
|
assert_eq!(e.dispatch(ReadReceiver), 4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user