diff --git a/crates/types/src/compound/enumerated.rs b/crates/types/src/compound/enumerated.rs index 60057c1..d2a9d05 100644 --- a/crates/types/src/compound/enumerated.rs +++ b/crates/types/src/compound/enumerated.rs @@ -1,5 +1,5 @@ use crate::compound::peano::{P0, Peano, PNext}; -use crate::compound::list::{self, Indexable, IntoList}; +use crate::compound::list::{self, Indexable, IntoList, List}; #[cfg(feature = "serde")] use serde::{Serialize, Deserialize}; @@ -19,6 +19,8 @@ pub trait DiscriminantCodable: Sized { } /// discriminant which encodes up to *but not including* P. +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "fmt", derive(Debug))] #[derive(Copy, Clone, Default, PartialEq)] pub struct Discr(u32, P::Unit); @@ -68,7 +70,7 @@ pub trait DiscrHandler { } /// helper used to call F with some (yet-to-be-determined) index of I -struct DispatchIndexable { +pub struct DispatchIndexable { indexable: I, f: F, } @@ -131,7 +133,9 @@ where #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "fmt", derive(Debug))] #[derive(Copy, Clone, Default, PartialEq)] -struct Enum(D, L); +pub struct Enum(D, L); + +pub type InternallyDiscriminated = Enum<(), List>; impl Enum<(Discr

,), L> { pub fn new(v: Variants) -> Self @@ -151,7 +155,7 @@ impl Enum<(), L> { } } -trait EnumRequirements { +pub trait EnumRequirements { type NumVariants: Peano; fn decode_discr(&self) -> Discr; fn encode_discr(&mut self, d: Discr); @@ -191,7 +195,7 @@ where Self: EnumRequirements { /// invoke the closure on the active variant, passing the variant by-value - fn dispatch<'a, F, R>(&'a self, f: F) -> R + pub fn dispatch<'a, F, R>(&'a self, f: F) -> R where DispatchIndexable<&'a L, F>: DiscrHandler<::NumVariants, R> { @@ -199,14 +203,14 @@ where } /// invoke the closure on the active variant, passing the variant by mutable reference - fn dispatch_mut<'a, F, R>(&'a mut self, f: F) -> R + pub fn dispatch_mut<'a, F, R>(&'a mut self, f: F) -> R where DispatchIndexable<&'a mut L, F>: DiscrHandler<::NumVariants, R> { self.decode_discr().dispatch(DispatchIndexable::new(&mut self.1, f)) } - fn set

(&mut self, value: L::Element) + pub fn set

(&mut self, value: L::Element) where P: Peano, L: Indexable

, @@ -310,8 +314,10 @@ mod test { #[test] fn internal_discr() { - let mut e: Enum<(), List<(BoxedF32, i32, u8)>> = Enum::default(); + type E = Enum<(), List<(BoxedF32, i32, u8)>>; + assert_eq!(::NumVariants::VALUE, 3); + let mut e: E = Enum::default(); assert_eq!(e.dispatch(ReadReceiver), 0); e.set::(BoxedF32(16f32)); @@ -329,7 +335,10 @@ mod test { #[test] fn new() { - let e = Enum::new((5u32, 4i32)); + type E = Enum<(Discr,), List<(u32, i32)>>; + assert_eq!(::NumVariants::VALUE, 2); + + let e: E = Enum::new((5u32, 4i32)); assert_eq!(e.dispatch(ReadReceiver), 5); let e = Enum::internally_discriminated((BoxedF32(4f32), -1i32));