Skip to content

Commit

Permalink
doc: Update documentation with example
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorlott committed Jul 22, 2023
1 parent 2e51f8d commit 06c6783
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ $ cargo add penum

## Latest feature

You can now use enum `discriminants` as expression blocks for `ToString`, `Display` and `Into<T>`.
You can now use enum `discriminants` as expression blocks for `ToString`, `Display`, `Into<T>` and `Deref<Target = T>`.
This could be useful as an alternative to const declarations. Read more about it below.


```rust
#[penum::to_string]
Expand All @@ -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<u32> } = {
Variant5 = EnumVariants::Variant0.to_string(),
Variant6 { list: Vec<u32> } = {
let string = list
.iter()
.map(ToString::to_string)
Expand All @@ -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<T>`.

- `penum::deref(T)` — Useful when you want to utilize Rust auto dereferencer.

- `penum::static_str` — Will implement `Deref<Str>` and `AsRef<str>`, 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
Expand Down
5 changes: 5 additions & 0 deletions src/factory/subject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = (&Ident, Comparable<Fields>)> {
self.get_variants()
.iter()
Expand Down
15 changes: 15 additions & 0 deletions src/penum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,22 @@ type PolyMap = PolymorphicMap<UniqueHashId<Type>, UniqueHashId<Type>>;
/// It contains everything we need to construct our dispatcher and
/// pattern validator.
pub struct Penum<State = Disassembled> {
/// 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<ItemImpl>,

/// Only used as a DX marker that seperates methods between Disassembled <> Assembled.
_marker: PhantomData<State>,
}

Expand All @@ -54,6 +65,9 @@ impl Penum<Disassembled> {
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(),
Expand Down Expand Up @@ -426,6 +440,7 @@ impl Penum<Assembled> {
}
}

// NOTE: I will eventually clean this mess up
pub trait Stringify: ToTokens {
fn get_string(&self) -> String {
self.to_token_stream().to_string()
Expand Down
12 changes: 4 additions & 8 deletions src/services.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -200,15 +199,12 @@ pub fn static_str(input: TokenStream) -> TokenStream {

quote::quote!(
impl AsRef<str> 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 }
}
)
}),
Expand Down

0 comments on commit 06c6783

Please sign in to comment.