Skip to content

Commit

Permalink
fix(macros): avoid comparing references
Browse files Browse the repository at this point in the history
if you use references, the compiler forces you to match on NOTHING: rust-lang/rust#78123

so yeah this fixes #5 woo hoo!
  • Loading branch information
onkoe committed Jul 8, 2024
1 parent 1cab648 commit 8966d1b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 2 additions & 4 deletions macros/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,18 @@ pub fn derive_error(input: DeriveInput) -> syn::Result<TokenStream2> {
// create each impl...

// error impl
let source = source(after_span, variants.iter())?; // TODO: check after_span
let source = source(after_span, &variants, name.clone())?; // TODO: check after_span
let description = description();
let cause = cause();

// display impl
let fmt = fmt(after_span, variants.iter(), name.clone())?;
let fmt = fmt(after_span, &variants, name.clone())?;

let error_path = create_path(input_span, &["std", "error", "Error"]);
let display_path = create_path(input_span, &["core", "fmt", "Display"]);

// put all those together!
let impl_block = quote_spanned! {after_span=>
use #error_path;

#[automatically_derived]
impl #error_path for #name {
#source
Expand Down
10 changes: 5 additions & 5 deletions macros/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
use quote::quote;
use syn::{Ident, Meta, Variant};
use syn::{punctuated::Punctuated, token::Comma, Ident, Meta, Variant};

use crate::util::create_path;

// TODO: check each variant and get info on their `#[error(...)]` attribute.

pub fn fmt<'a>(
pub fn fmt(
span: Span2,
variants: impl Iterator<Item = &'a Variant>,
variants: &Punctuated<Variant, Comma>,
enum_ident: Ident,
) -> syn::Result<TokenStream2> {
// just an attribute that looks like `#[error(...)]`.
Expand Down Expand Up @@ -36,7 +36,7 @@ pub fn fmt<'a>(
let variant_ident = &v.ident;
let tokens = &attr_args.tokens;
vec.push(quote! {
&#enum_ident::#variant_ident => {f.write_str(format!(#tokens).as_str())},
#enum_ident::#variant_ident => {f.write_str(format!(#tokens).as_str())},
});
} else {
return Err(syn::Error::new_spanned(
Expand All @@ -49,7 +49,7 @@ pub fn fmt<'a>(

Ok(quote! {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
match *self {
#(#vec)*
}
}
Expand Down
14 changes: 8 additions & 6 deletions macros/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
use quote::quote;
use syn::Variant;
use syn::{punctuated::Punctuated, token::Comma, Ident, Variant};

use crate::util::create_path;

// pub fn create_error_impl() { // TODO?: call all these fns in here! }

/// Parses the user's enum's variants to check for any internal `#[from]`
/// attributes, then generates code that matches on any given error variant.
pub fn source<'a>(
pub fn source(
span: Span2,
variants: impl Iterator<Item = &'a Variant>,
variants: &Punctuated<Variant, Comma>,
enum_ident: Ident,
) -> syn::Result<TokenStream2> {
let from_attr = create_path(span, &["from"]);

// make a new hashmap to store variants' attribute field, if it's even there!
// store each variant's match arm, if it's even there!
let mut vec = vec![];

// check for any `from` attributes on variants
for v in variants {
let mut t = None;
let mut t = quote! { None };
for f in &v.fields {
// if any of a variant's fields have the from attribute...
if f.attrs.iter().any(|attr| *attr.meta.path() == from_attr) {
// ...use that field in the source method impl
t = Some(f.ty.clone());
let ty = f.ty.clone();
t = quote! { Some(#ty) };
}
}

Expand Down

0 comments on commit 8966d1b

Please sign in to comment.