diff --git a/README.md b/README.md index d3c1c27..6c24aa3 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,9 @@ $ cargo add penum ## Latest feature -You can now use enum `discriminants` as expression blocks for `ToString`, `Display` and `Into`. +You can now use enum `discriminants` as expression blocks for `ToString`, `Display`, `Into` and `Deref`. +This could be useful as an alternative to const declarations. Read more about it below. + ```rust #[penum::to_string] @@ -61,7 +63,8 @@ enum EnumVariants { Variant2(i32, u32) = stringify!(f0, f1).to_string(), Variant3 { name: String } = format!("My string {name}"), Variant4 { age: u32 } = age.to_string(), - Variant5 { list: Vec } = { + Variant5 = EnumVariants::Variant0.to_string(), + Variant6 { list: Vec } = { let string = list .iter() .map(ToString::to_string) @@ -75,8 +78,20 @@ let enum_variants = Enum::Variant0; println!("{}", enum_variants.to_string()); ``` -Add the attribute `#[penum::to_string]` or `#[penum::fmt]` to replace strict enum descriminant. +Add one of the following to your enum to enable enum descriminant expressions. +- `penum::to_string` — Useful when you only want to implement `ToString`. + +- `penum::fmt` — Useful when you want to implement `ToString` and `Display`. + +- `penum::into(T)` — Useful when you want to convert your variant `Into`. + +- `penum::deref(T)` — Useful when you want to utilize Rust auto dereferencer. + +- `penum::static_str` — Will implement `Deref` and `AsRef`, including helper methods + like: `.as_str()` and `.static_str()`. +Make sure to also try out `penum::penum` if you like this `feature`. Note that not interoperable +with `penum::penum`, and should be used separatly, because they are mutually exclusive. ------------------------------------------------------------------ ## Overview diff --git a/src/factory/subject.rs b/src/factory/subject.rs index 1b5f4e6..6d418d7 100644 --- a/src/factory/subject.rs +++ b/src/factory/subject.rs @@ -26,6 +26,11 @@ impl Subject { &self.data.variants } + /// This will basically break each variant into two parts, VariantIdent and a Comparable. A + /// Comparable will eventually pair up with another Comparable to create a ComparablePair. + /// + /// This intermediate construct is used to extract fields that will be used multiple times during + /// compairs. pub fn comparable_fields_iter(&self) -> impl Iterator)> { self.get_variants() .iter() diff --git a/src/penum.rs b/src/penum.rs index b30e78d..eefc6f4 100644 --- a/src/penum.rs +++ b/src/penum.rs @@ -41,11 +41,22 @@ type PolyMap = PolymorphicMap, UniqueHashId>; /// It contains everything we need to construct our dispatcher and /// pattern validator. pub struct Penum { + /// A Penum expression consists of one or more patterns, and an optional WhereClause. expr: PenumExpr, + + /// The enum (or ADT in the future) that we will read and specialize. subject: Subject, + + /// A simple macro diagnostic struct that we use to append compiler errors with span information. error: Diagnostic, + + /// I use this to map generics to concrete types that I then can use during substitution stage. types: PolyMap, + + /// Contains all the impls that we've managed to construct. impls: Vec, + + /// Only used as a DX marker that seperates methods between Disassembled <> Assembled. _marker: PhantomData, } @@ -54,6 +65,9 @@ impl Penum { Self { expr, subject, + // It's kind of annoying that I have to impl `Default` for `expr` and `subject` for the + // spread operator to work `..Default::default()` + // NOTE: I could extract these fields into another struct. error: Default::default(), types: Default::default(), impls: Default::default(), @@ -426,6 +440,7 @@ impl Penum { } } +// NOTE: I will eventually clean this mess up pub trait Stringify: ToTokens { fn get_string(&self) -> String { self.to_token_stream().to_string() diff --git a/src/services.rs b/src/services.rs index 50f4252..494bff3 100644 --- a/src/services.rs +++ b/src/services.rs @@ -1,9 +1,8 @@ use proc_macro::TokenStream; use quote::ToTokens; use syn::parse_macro_input; -use syn::Type; - use syn::ItemTrait; +use syn::Type; use crate::dispatch::T_SHM; use crate::factory::PenumExpr; @@ -200,15 +199,12 @@ pub fn static_str(input: TokenStream) -> TokenStream { quote::quote!( impl AsRef for #enum_name { - fn as_ref(&self) -> &str { - &**self - } + fn as_ref(&self) -> &str { &**self } } impl #enum_name { - fn as_str(&self) -> &str { - &**self - } + fn as_str(&self) -> &str { &**self } + fn static_str(&self) -> &str { &**self } } ) }),