Destruct structs and enums into a heterogeneous list consists of a fixed set of types, to enable a quick implementation of combinator libraries.
trait Destructtype DestructTypeThe destructed object type
If your struct is: ```rust
struct YourStruct { field: YourField, field2: YourField2, } ``` Then the DestructType is:
DestructBegin<Fields, m>
where Fields = DestructField<YourField, NextField, m1>
NextField = DestructField<YourField2, End, m2>
End = DestructEnd<m>
where m is some generated type implementing `trait DestructMetadata`
m1 is the metadata for `field`, implementing `trait DestructFieldMetadata + DestructMetadata`
m2 is the metadata for `field2`, implementing `trait DestructFieldMetadata + DestructMetadata`
}
Here is a list of types may appear in DestructType:
fn destruct(self) -> Self::DestructTypeDestruct self to destruct type
fn construct(d: Self::DestructType) -> Self;Construct self from destruct type
```rust pub trait DestructMetadata { fn structname() -> &'static str; fn namedfields() -> bool; }
pub trait DestructFieldMetadata: DestructMetadata + 'static { fn fieldname() -> &'static str; fn fieldindex() -> usize; }
pub trait DestructEnumMetadata { fn enum_name() -> &'static str; }
pub trait DestructEnumVariantMetadata: DestructEnumMetadata + 'static { fn variantname() -> &'static str; fn variantindex() -> usize; } ```
For example, here is how to implement a parser with destruct (see destruct-parser):
Parsable trait;Destruct for your struct by adding #[derive(Destruct)]impl<T: Destruct> Parsable, namely parsable!;Parsable for your struct by adding #[destruct(parsable)]parsable! macro?Because Rust forbids overlapping implementation of traits. Ideally what I need is the following trait implementation:
rust
impl<T: Destruct> Parsable for T where T::DestructType: Parsable {}
But Rust complains:
upstream crates may add new impl of trait `destruct::Destruct` for type `destruct::DestructEnumBegin<_, _>` in future versions
So I added #[destruct(parsable)] to generate impls for every struct. It is equivalent to parsable!(YourStruct).
```rust
macro_rules! parsable {
($t:ident) => {
impl Parsable for $t {
fn parse
Generics are not supported.