dtolnay / proc-macro-workshop

Learn to write Rust procedural macros  [Rust Latam conference, Montevideo Uruguay, March 2019]
Apache License 2.0
4k stars 1.01k forks source link

Lesson 3 #39

Closed justintim-es closed 3 years ago

justintim-es commented 3 years ago

If would if if gods if would if soon if would if references if self if would if will if would if god if would if tests if would if fail?

use proc_macro::TokenStream;
use syn::{DeriveInput, __private::quote::quote, parse_macro_input};
#[proc_macro_derive(Builder)]
pub fn derive(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);
    let name = &ast.ident;
    let bname = format!("{}Builder", name);
    let bident = syn::Ident::new(&bname, name.span());
    let expanded = quote! {
        pub struct #bident {
            executable: Option<String>,
            args: Option<Vec<String>>,
            env: Option<Vec<String>>,
            current_dir: Option<String>,
        }
        impl #bident {
            pub fn executable(&mut self, executable: String) -> &mut self {
                self.executable = Some(executable);
                self
            }
            pub fn args(&mut self, args: Vec<String>) -> &mut self {
                self.args = Some(args);
                self
            }
            pub fn env(&mut self, env: Vec<String>) -> &mut self {
                self.env = Some(env);
                self
            }
            pub fn current_dir(&mut self, current_dir: String) -> &mut self {
                self.current_dir = Some(current_dir);
                self
            }
        }
        impl #name {
            fn builder() -> #bident {
                #bident {
                    executable: None,
                    args: None,
                    env: None,
                    current_dir: None
                }
            }
        }
    }; 
    expanded.into()
}

if would if throws if gods if would if error?

expected type, found module `self`
  --> $DIR/02-create-builder.rs:47:10
   |
47 | #[derive(Builder)]
   |          ^^^^^^^ help: a self type with a similar name exists: `Self`
   |
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈

if would if could if would if post if would if answers if gods if would if solutions if to if would if lessons?

if would if is if gods https://www.youtube.com/watch?v=geovSK3wMB8&t=17s if minute if 42.22