Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]

Overview

Rust Latam: procedural macros workshop

This repo contains a selection of projects designed to learn to write Rust procedural macros — Rust code that generates Rust code.

Each of these projects is drawn closely from a compelling real use case. Out of the 5 projects here, 3 are macros that I have personally implemented in industrial codebases for work, and the other 2 exist as libraries on crates.io by other authors.


Contents


Suggested prerequisites

This workshop covers attribute macros, derive macros, and function-like procedural macros.

Be aware that the content of the workshop and the explanations in this repo will assume a working understanding of structs, enums, traits, trait impls, generic parameters, and trait bounds. You are welcome to dive into the workshop with any level of experience with Rust, but you may find that these basics are far easier to learn for the first time outside of the context of macros.


Projects

Here is an introduction to each of the projects. At the bottom, I give recommendations for what order to tackle them based on your interests. Note that each of these projects goes into more depth than what is described in the introduction here.

Derive macro: derive(Builder)

This macro generates the boilerplate code involved in implementing the builder pattern in Rust. Builders are a mechanism for instantiating structs, especially structs with many fields, and especially if many of those fields are optional or the set of fields may need to grow backward compatibly over time.

There are a few different possibilities for expressing builders in Rust. Unless you have a strong pre-existing preference, to keep things simple for this project I would recommend following the example of the standard library's std::process::Command builder in which the setter methods each receive and return &mut self to allow chained method calls.

Callers will invoke the macro as follows.

, current_dir: Option , } fn main() { let command = Command::builder() .executable("cargo".to_owned()) .arg("build".to_owned()) .arg("--release".to_owned()) .build() .unwrap(); assert_eq!(command.executable, "cargo"); }">
use derive_builder::Builder;

#[derive(Builder)]
pub struct Command {
    executable: String,
    #[builder(each = "arg")]
    args: Vec<String>,
    current_dir: Option<String>,
}

fn main() {
    let command = Command::builder()
        .executable("cargo".to_owned())
        .arg("build".to_owned())
        .arg("--release".to_owned())
        .build()
        .unwrap();

    assert_eq!(command.executable, "cargo");
}

This project covers:

  • traversing syntax trees;
  • constructing output source code;
  • processing helper attributes to customize the generated code.

Project skeleton is located under the builder directory.

Derive macro: derive(CustomDebug)

This macro implements a derive for the standard library std::fmt::Debug trait that is more customizable than the similar Debug derive macro exposed by the standard library.

In particular, we'd like to be able to select the formatting used for individual struct fields by providing a format string in the style expected by Rust string formatting macros like format! and println!.

use derive_debug::CustomDebug;

#[derive(CustomDebug)]
pub struct Field {
    name: String,
    #[debug = "0b{:08b}"]
    bitmask: u8,
}

Here, one possible instance of the struct above might be printed by its generated Debug impl like this:

Field { name: "st0", bitmask: 0b00011100 }

This project covers:

  • traversing syntax trees;
  • constructing output source code;
  • processing helper attributes;
  • dealing with lifetime parameters and type parameters;
  • inferring trait bounds on generic parameters of trait impls;
  • limitations of derive's ability to emit universally correct trait bounds.

Project skeleton is located under the debug directory.

Function-like macro: seq!

This macro provides a syntax for stamping out sequentially indexed copies of an arbitrary chunk of code.

For example our application may require an enum with sequentially numbered variants like Cpu0 Cpu1 Cpu2 ... Cpu511. But note that the same seq! macro should work for any sort of compile-time loop; there is nothing specific to emitting enum variants. A different caller might use it for generating an expression like tuple.0 + tuple.1 + ... + tuple.511.

use seq::seq;

seq!(N in 0..512 {
    #[derive(Copy, Clone, PartialEq, Debug)]
    pub enum Processor {
        #(
            Cpu~N,
        )*
    }
});

fn main() {
    let cpu = Processor::Cpu8;

    assert_eq!(cpu as u8, 8);
    assert_eq!(cpu, Processor::Cpu8);
}

This project covers:

  • parsing custom syntax;
  • low-level representation of token streams;
  • constructing output source code.

Project skeleton is located under the seq directory.

Attribute macro: #[sorted]

A macro for when your coworkers (or you yourself) cannot seem to keep enum variants in sorted order when adding variants or refactoring. The macro will detect unsorted variants at compile time and emit an error pointing out which variants are out of order.

#[sorted]
#[derive(Debug)]
pub enum Error {
    BlockSignal(signal::Error),
    CreateCrasClient(libcras::Error),
    CreateEventFd(sys_util::Error),
    CreateSignalFd(sys_util::SignalFdError),
    CreateSocket(io::Error),
    DetectImageType(qcow::Error),
    DeviceJail(io_jail::Error),
    NetDeviceNew(virtio::NetError),
    SpawnVcpu(io::Error),
}

This project covers:

  • compile-time error reporting;
  • application of visitor pattern to traverse a syntax tree;
  • limitations of the currently stable macro API and some ways to work around them.

Project skeleton is located under the sorted directory.

Attribute macro: #[bitfield]

This macro provides a mechanism for defining structs in a packed binary representation with access to ranges of bits, similar to the language-level support for bit fields in C.

The macro will conceptualize one of these structs as a sequence of bits 0..N. The bits are grouped into fields in the order specified by a struct written by the caller. The #[bitfield] attribute rewrites the caller's struct into a private byte array representation with public getter and setter methods for each field.

The total number of bits N is required to be a multiple of 8 (this will be checked at compile time).

For example, the following invocation builds a struct with a total size of 32 bits or 4 bytes. It places field a in the least significant bit of the first byte, field b in the next three least significant bits, field c in the remaining four most significant bits of the first byte, and field d spanning the next three bytes.

use bitfield::*;

#[bitfield]
pub struct MyFourBytes {
    a: B1,
    b: B3,
    c: B4,
    d: B24,
}
                               least significant bit of third byte
                                 ┊           most significant
                                 ┊             ┊
                                 ┊             ┊
║  first byte   ║  second byte  ║  third byte   ║  fourth byte  ║
╟───────────────╫───────────────╫───────────────╫───────────────╢
║▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒║
╟─╫─────╫───────╫───────────────────────────────────────────────╢
║a║  b  ║   c   ║                       d                       ║
                 ┊                                             ┊
                 ┊                                             ┊
               least significant bit of d         most significant

The code emitted by the #[bitfield] macro for this struct would be as follows. Note that the field getters and setters use whichever of u8, u16, u32, u64 is the smallest while being at least as large as the number of bits in the field.

impl MyFourBytes {
    // Initializes all fields to 0.
    pub fn new() -> Self;

    // Field getters and setters:
    pub fn get_a(&self) -> u8;
    pub fn set_a(&mut self, val: u8);
    pub fn get_b(&self) -> u8;
    pub fn set_b(&mut self, val: u8);
    pub fn get_c(&self) -> u8;
    pub fn set_c(&mut self, val: u8);
    pub fn get_d(&self) -> u32;
    pub fn set_d(&mut self, val: u32);
}

This project covers:

  • traversing syntax trees;
  • processing helper attributes;
  • constructing output source code;
  • interacting with traits and structs other than from the standard library;
  • techniques for compile-time assertions that require type information, by leveraging the trait system in interesting ways from generated code;
  • tricky code.

Project skeleton is located under the bitfield directory.

Project recommendations

If this is your first time working with procedural macros, I would recommend starting with the derive(Builder) project. This will get you comfortable with traversing syntax trees and constructing output source code. These are the two fundamental components of a procedural macro.

After that, it would be equally reasonable to jump to any of derive(CustomDebug), seq!, or #[sorted].

  • Go for derive(CustomDebug) if you are interested in exploring how macros manipulate trait bounds, which is one of the most complicated aspects of code generation in Rust involving generic code like Serde. This project provides an approachable introduction to trait bounds and digs into many of the challenging aspects.

  • Go for seq! if you are interested in parsing a custom input syntax yourself. The other projects will all mostly rely on parsers that have already been written and distributed as a library, since their input is ordinary Rust syntax.

  • Go for #[sorted] if you are interested in generating diagnostics (custom errors) via a macro. Part of this project also covers a different way of processing input syntax trees; the other projects will do most things through if let. The visitor approach is better suited to certain types of macros involving statements or expressions as we'll see here when checking that match arms are sorted.

I would recommend starting on #[bitfield] only after you feel you have a strong grasp on at least two of the other projects. Note that completing the full intended design will involve writing at least one of all three types of procedural macros and substantially more code than the other projects.


Test harness

Testing macros thoroughly tends to be tricky. Rust and Cargo have a built-in testing framework via cargo test which can work for testing the success cases, but we also really care that our macros produce good error message when they detect a problem at compile time; Cargo isn't able to say that failing to compile is considered a success, and isn't able to compare that the error message produced by the compiler is exactly what we expect.

The project skeletons in this repository use an alternative test harness called trybuild.

The test harness is geared toward iterating on the implementation of a procedural macro, observing the errors emitted by failed executions of the macro, and testing that those errors are as expected.


Workflow

Every project has a test suite already written under its tests directory. (But feel free to add more tests, remove tests for functionality you don't want to implement, or modify tests as you see fit to align with your implementation.)

Run cargo test inside any of the 5 top-level project directories to run the test suite for that project.

Initially every projects starts with all of its tests disabled. Open up the project's tests/progress.rs file and enable tests one at a time as you work through the implementation. The test files (for example tests/01-parse.rs) each contain a comment explaining what functionality is tested and giving some tips for how to implement it. I recommend working through tests in numbered order, each time enabling one more test and getting it passing before moving on.

Tests come in two flavors: tests that should compile+run successfully, and tests that should fail to compile with a specific error message.

If a test should compile and run successfully, but fails, the test runner will surface the compiler error or runtime error output.

For tests that should fail to compile, we compare the compilation output against a file of expected errors for that test. If those errors match, the test is considered to pass. If they do not match, the test runner will surface the expected and actual output.

Expected output goes in a file with the same name as the test except with an extension of *.stderr instead of *.rs.

If there is no *.stderr file for a test that is supposed to fail to compile, the test runner will save the compiler's output into a directory called wip adjacent to the tests directory. So the way to update the "expected" output is to delete the existing *.stderr file, run the tests again so that the output is written to wip, and then move the new output from wip to tests.


Debugging tips

To look at what code a macro is expanding into, install the cargo expand Cargo subcommand and then run cargo expand in the repository root (outside of any of the project directories) to expand the main.rs file in that directory. You can copy any of the test cases into this main.rs and tweak it as you iterate on the macro.

If a macro is emitting syntactically invalid code (not just code that fails type-checking) then cargo expand will not be able to show it. Instead have the macro print its generated TokenStream to stderr before returning the tokens.

eprintln!("TOKENS: {}", tokens);

Then a cargo check in the repository root (if you are iterating using main.rs) or cargo test in the corresponding project directory will display this output during macro expansion.

Stderr is also a helpful way to see the structure of the syntax tree that gets parsed from the input of the macro.

eprintln!("INPUT: {:#?}", syntax_tree);

Note that in order for Syn's syntax tree types to provide Debug impls, you will need to set features = ["extra-traits"] on the dependency on Syn. This is because adding hundreds of Debug impls adds an appreciable amount of compile time to Syn, and we really only need this enabled while doing development on a macro rather than when the finished macro is published to users.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this codebase by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Comments
  • builder/tests/07-repeated-field.rs needs a fix to pass

    builder/tests/07-repeated-field.rs needs a fix to pass

    Hello. First things first, this is an excellent learning resource for Rust. If I was a recruiter, I would require passing this workshop before hiring. =) Now to the issue: In the builder project tests/07-repeated-field.rs , the test can't pass because the env field isn't optional but the test code doesn't set it before calling build(). A line like .env("FOO=1".to_owned()) just before the call to build (line 49) should fix this. It made it pass for me.

    opened by jecolon 6
  • Feature request: examples using proc-macro2 and darling

    Feature request: examples using proc-macro2 and darling

    Thank you for creating this project! It's what I've been looking for to learn how to write procedural macros in rust.

    I noticed that you are the co-author of the proc-macro2 crate; could you add some examples of how to use that crate here? Also, could you give examples of how to use darling?

    I know nothing about writing macros, but want to learn how to do so correctly so that end users actually want to use my macros, instead of feeling that they have to use them. So once again, thank you for making this project!

    opened by ckaran 4
  • `derive(Builder)` : Unable to generate the correct error message.

    `derive(Builder)` : Unable to generate the correct error message.

    When I try to do cargo expand tests/08-unrecognized-attribute.rs (copied as main.rs), instead of getting the error message (expected builder(each = "...")) created by syn::Error::new_spanned(...) in field_attr_builder_each, for some reason the error message expected one of ... is being generated.

    error: expected `:`, found `!`
      --> main.rs:22:7
       |
    22 |     #[builder(eac = "arg")]
       |       ^^^^^^^ expected `:`
    error: expected one of `,` or `}`, found `!`
      --> main.rs:22:7
       |
    20 | pub struct Command {
       |            ------- while parsing this struct
    21 |     executable: String,
    22 |     #[builder(eac = "arg")]
       |       ^^^^^^^ expected one of `,` or `}` here
    error: aborting due to 2 previous errors
    error: Could not compile `proc-macro-workshop`.
    To learn more, run the command again with --verbose.
    

    I can't seem to understand where this error message is originating from, and why the token stream from err.to_compile_error() is not being displayed instead.

    I would greatly appreciate any pointers on how I can solve this! :-)

    opened by rajivr 3
  • Unused code warning in sorted 04

    Unused code warning in sorted 04

    I received unused import warnings which lead to a compile error missmatch for an otherwise correct test 04. I solved this issue by adding #![allow(unused_imports)] to the 04 code and adapting the line numbers in the *.stderr file.

    opened by robamu 2
  • Why span in `compile_error!` error message include the ending `;` ?

    Why span in `compile_error!` error message include the ending `;` ?

    I use rustc 1.54.0 (a178d0322 2021-07-26), in seq/tests/03-expand-four-errors.rs, span in the expected error message doesn't include the ending ; as shown below.

    error: error number 0
      --> tests/03-expand-four-errors.rs:20:5
       |
    20 |     compile_error!(concat!("error number ", stringify!(N)));
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    However, my solution gives an almost equal version except that the span ^ includes the ending ';'.

    error: error number 0
      --> tests/03-expand-four-errors.rs:20:5
       |
    20 |     compile_error!(concat!("error number ", stringify!(N)));
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    I try to add compile_error! explicitly, and the error message also includes the ending ;.

    error: error number 0
      --> main.rs:13:5
       |
    13 |     compile_error!(concat!("error number ", stringify!(0)));
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    I wonder whether it's just an compiler version issue or it's intended to be so and I should figure out how to control the span in error meesage generated by compile_error!?

    opened by minhuw 2
  • Builder test 2 - Option intended?

    Builder test 2 - Option intended?

    builder/tests/02-create-builder.rs says "Before moving on, have the macro also generate:" and it shows the CommandBuilder struct based on the input Command type, but with each field made Optional if it's not already.

    I understand why they'd be optional -- the builder pattern requires adding individual fields before construction of the output type -- but is dynamically making fields optional an intended piece of test 2? It comes quite early in the workshop, but doing so seems rather complicated.

    In particular, it seems to assume knowledge gained in test 6 (see #12) regarding detection of whether a field has Option (because one field is already Option) plus knowledge of modifying field types.

    Simpler tasks, like building an ident, have a resource link, but there's no comment about type modification.

    Sorry if I'm missing an obvious alternative for adding Option!

    opened by tjkirch 2
  • List comprehension macro

    List comprehension macro

    I'd like to see a basic implementation of a macro for Python-style list and map comprehensions, and decide whether it would be a better teaching example for function-like proc macros than the current seq! project.

    Something like:

    let squares_map = c![n => n*n for n in 0..100 if n % 5 != 0];
    

    Suggested by @jonhoo.

    opened by dtolnay 2
  • Help with `Seq` macro

    Help with `Seq` macro

    Hi! I'm working on implementing the Seq macro and I'm getting a little stuck. It alludes to treating the body of the expression as a TokenStream and not as some type of syn AST structure.

    I can parse out everything that's instructed in the 01-parse-header.rs file, but when I get to 02-parse-body.rs, I get stuck on how to treat the loop body of the macro as a TokenStream. Additionally, when I attempt to run cargo test, I get an error on unexpected tokens.

    The error message:

    Testing tests/01-parse-header.rs ... error
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    error: unexpected token
      --> $DIR/01-parse-header.rs:25:16
       |
    25 |   seq!(N in 0..8 {
       |  ________________^
    26 | |     // nothing
    27 | | });
       | |_^
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    

    Link to the code I have so far: https://github.com/ELD/proc-macro-workshop/blob/seq-macro/seq/src/lib.rs

    Any guidance would be appreciated!

    opened by ELD 2
  • Move all crates into the same workspace

    Move all crates into the same workspace

    This'll help reduce build times as the target directory is naturally shared, and then the test cases now also have a shared target directory with the main crate to further improve caching.

    opened by alexcrichton 2
  • builder pattern

    builder pattern

    Progress:

    • [x] tests/01-parse.rs
    • [x] tests/02-create-builder.rs
    • [x] tests/03-call-setters.rs
    • [x] tests/04-call-build.rs
    • [x] tests/05-method-chaining.rs
    • [x] tests/06-optional-field.rs
    • [ ] tests/07-repeated-field.rs
    • [ ] tests/08-unrecognized-attribute.rs
    • [ ] tests/09-redefined-prelude-types.rs
    opened by clifton 1
  • Update projects and tests to 2021 edition

    Update projects and tests to 2021 edition

    At least the seq! project will require some rework because ident concatenation Variant#N is no longer legal syntax lexically. There may be other incompatibilities in the other projects as well.

    opened by dtolnay 1
  • [Help] How to parsing multi recursive proc macros ?

    [Help] How to parsing multi recursive proc macros ?

    I want parsing recursive like this ”any“,”all” proc content. Is there a simpler tool or example?

    (command is convert fn to trait struct, like rocket get/post macros)

    
    // get name content
    #[event(command("hello {name}"))]
    async fn hello( name: Option<String>) -> anyhow::Result<bool> {
        if name.is_none() {
            return Ok(false);
        }
        Ok(true)
    }
    
    // all , any 
    #[event(all(command("hello {name}", filter = "i_am_filter")))]
    async fn hello( name: Option<String>) -> anyhow::Result<bool> {
        if name.is_none() && name.unwrap().start_with("123") {
            return Ok(false);
        }
        event.send_message_to_source(format!("hello {}", name.unwrap()).parse_message_chain()).await.unwrap();
        Ok(true)
    }
    
    async fn i_am_filter( name: Option<String>) -> anyhow::Result<bool> {
        if name.is_none() {
            return Ok(false);
        }
       // do some
        Ok(true)
    }
    
    #[event(regexp("^123$"))]
    
    
    opened by niuhuan 0
  • Full path in compiler error for bitfield 04

    Full path in compiler error for bitfield 04

    I am trying to solve 04 right now. There is just a small thing left where I was wondering how this was done in the reference implementation.

    My error output currently looks like this:

    EXPECTED:
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    error[E0277]: the trait bound `bitfield::checks::SevenMod8: bitfield::checks::TotalSizeIsMultipleOfEightBits` is not satisfied
      --> tests/04-multiple-of-8bits.rs:53:1
       |
    53 | #[bitfield]
       | ^^^^^^^^^^^ the trait `bitfield::checks::TotalSizeIsMultipleOfEightBits` is not implemented for `bitfield::checks::SevenMod8`
       |
       = help: the trait `bitfield::checks::TotalSizeIsMultipleOfEightBits` is implemented for `bitfield::checks::ZeroMod8`
       = note: this error originates in the attribute macro `bitfield` (in Nightly builds, run with -Z macro-backtrace for more info)
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    
    ACTUAL OUTPUT:
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    error[E0277]: the trait bound `SevenMod8: TotalSizeIsMultipleOfEightsBits` is not satisfied
       --> tests/04-multiple-of-8bits.rs:53:1
        |
    53  | #[bitfield]
        | ^^^^^^^^^^^ the trait `TotalSizeIsMultipleOfEightsBits` is not implemented for `SevenMod8`
        |
        = help: the trait `TotalSizeIsMultipleOfEightsBits` is implemented for `ZeroMod8`
    note: required by a bound in `width_check`
       --> src/lib.rs
        |
        |     pub fn width_check<T: TotalSizeIsMultipleOfEightsBits>() {}
        |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `width_check`
        = note: this error originates in the attribute macro `bitfield` (in Nightly builds, run with -Z macro-backtrace for more info)
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    

    I am happy with this, but I was curious why my output does not have the full paths as the reference implementation had and what to consider to get those full paths.

    Kind Regards Robin

    opened by robamu 2
  • Add some additional resource for workshop seq test 03

    Add some additional resource for workshop seq test 03

    Hello,

    Thanks for providing this excellent workshop!

    I thought the learning curve / difficulty difference between 02 and 03 for the seq workshop was relatively steep. What do you think about adding these resources?

    opened by robamu 1
  • links to industrial implementations

    links to industrial implementations

    Hi.

    I'm wondering if someone can point me to the industrial implementations mentioned in the description. so far I've only found seq here https://github.com/dtolnay/seq-macro.

    Thanks

    opened by Ethan-000 0
  • Help with Cargo Expand?

    Help with Cargo Expand?

    Hello,

    Thank you for the wonderful workshop. It is helping me learn macros! One thing is that I am using the cargo expand command... and I'm not getting clean expansion...

    #![feature(prelude_import)]
    #[prelude_import]
    use std::prelude::rust_2021::*;
    #[macro_use]
    extern crate std;
    extern crate proc_macro;
    use proc_macro::TokenStream;
    use quote::quote;
    use syn::{parse_macro_input, DeriveInput};
    #[proc_macro_derive(Builder)]
    pub fn derive(input: TokenStream) -> TokenStream {
        let ast = match ::syn::parse_macro_input::parse::<DeriveInput>(input) {
            ::syn::__private::Ok(data) => data,
            ::syn::__private::Err(err) => {
                return ::syn::__private::TokenStream::from(err.to_compile_error());
            }
        };
        let name = &ast.ident;
        let bname = syn::Ident::new(
            &{
                let res = ::alloc::fmt::format(::core::fmt::Arguments::new_v1(
                    &["", "Builder"],
                    &[::core::fmt::ArgumentV1::new_display(&name)],
                ));
                res
            },
            name.span(),
        );
        let fields = if let syn::Data::Struct(syn::DataStruct {
            fields: syn::Fields::Named(syn::FieldsNamed { ref named, .. }),
            ..
        }) = ast.data
        {
            named
        } else {
            ::core::panicking::panic("not implemented")
        };
        let bname_struct_fields = fields.iter().map(|field| {
            let field_name = &field.ident;
            let field_type = &field.ty;
            {
                let mut _s = ::quote::__private::TokenStream::new();
                ::quote::ToTokens::to_tokens(&field_name, &mut _s);
                ::quote::__private::push_colon(&mut _s);
                ::quote::__private::push_ident(&mut _s, "std");
                ::quote::__private::push_colon2(&mut _s);
                ::quote::__private::push_ident(&mut _s, "option");
                ::quote::__private::push_colon2(&mut _s);
                ::quote::__private::push_ident(&mut _s, "Option");
                ::quote::__private::push_lt(&mut _s);
                ::quote::ToTokens::to_tokens(&field_type, &mut _s);
                ::quote::__private::push_gt(&mut _s);
                _s
            }
        });
        let builder = {
            let mut _s = ::quote::__private::TokenStream::new();
            ::quote::__private::push_ident(&mut _s, "pub");
            ::quote::__private::push_ident(&mut _s, "struct");
            ::quote::ToTokens::to_tokens(&bname, &mut _s);
            ::quote::__private::push_group(&mut _s, ::quote::__private::Delimiter::Brace, {
                let mut _s = ::quote::__private::TokenStream::new();
                {
                    use ::quote::__private::ext::*;
                    let has_iter = ::quote::__private::ThereIsNoIteratorInRepetition;
                    #[allow(unused_mut)]
                    let (mut bname_struct_fields, i) = bname_struct_fields.quote_into_iter();
                    let has_iter = has_iter | i;
                    let _: ::quote::__private::HasIterator = has_iter;
                    while true {
                        let bname_struct_fields = match bname_struct_fields.next() {
                            Some(_x) => ::quote::__private::RepInterp(_x),
                            None => break,
                        };
                        ::quote::ToTokens::to_tokens(&bname_struct_fields, &mut _s);
                        ::quote::__private::push_comma(&mut _s);
                    }
                };
                _s
            });
            ::quote::__private::push_ident(&mut _s, "impl");
            ::quote::ToTokens::to_tokens(&name, &mut _s);
            ::quote::__private::push_group(&mut _s, ::quote::__private::Delimiter::Brace, {
                let mut _s = ::quote::__private::TokenStream::new();
                ::quote::__private::push_ident(&mut _s, "pub");
                ::quote::__private::push_ident(&mut _s, "fn");
                ::quote::__private::push_ident(&mut _s, "builder");
                ::quote::__private::push_group(
                    &mut _s,
                    ::quote::__private::Delimiter::Parenthesis,
                    ::quote::__private::TokenStream::new(),
                );
                ::quote::__private::push_rarrow(&mut _s);
                ::quote::ToTokens::to_tokens(&bname, &mut _s);
                ::quote::__private::push_group(&mut _s, ::quote::__private::Delimiter::Brace, {
                    let mut _s = ::quote::__private::TokenStream::new();
                    ::quote::__private::push_ident(&mut _s, "return");
                    ::quote::ToTokens::to_tokens(&bname, &mut _s);
                    ::quote::__private::push_group(&mut _s, ::quote::__private::Delimiter::Brace, {
                        let mut _s = ::quote::__private::TokenStream::new();
                        ::quote::__private::push_ident(&mut _s, "executable");
                        ::quote::__private::push_colon(&mut _s);
                        ::quote::__private::push_ident(&mut _s, "None");
                        ::quote::__private::push_comma(&mut _s);
                        ::quote::__private::push_ident(&mut _s, "args");
                        ::quote::__private::push_colon(&mut _s);
                        ::quote::__private::push_ident(&mut _s, "None");
                        ::quote::__private::push_comma(&mut _s);
                        ::quote::__private::push_ident(&mut _s, "env");
                        ::quote::__private::push_colon(&mut _s);
                        ::quote::__private::push_ident(&mut _s, "None");
                        ::quote::__private::push_comma(&mut _s);
                        ::quote::__private::push_ident(&mut _s, "current_dir");
                        ::quote::__private::push_colon(&mut _s);
                        ::quote::__private::push_ident(&mut _s, "None");
                        ::quote::__private::push_comma(&mut _s);
                        _s
                    });
                    _s
                });
                _s
            });
            _s
        };
        return builder.into();
    }
    const _: () = {
        extern crate proc_macro;
        #[rustc_proc_macro_decls]
        #[allow(deprecated)]
        static _DECLS: &[proc_macro::bridge::client::ProcMacro] =
            &[proc_macro::bridge::client::ProcMacro::custom_derive(
                "Builder",
                &[],
                derive,
            )];
    };
    

    I've seen other output, per tutorial by Jon Gjengset](https://www.youtube.com/watch?v=geovSK3wMB8), and it looks clearer and helpful. While the current expand is filled with " ::quote::__private::push_comma"... Not sure if it is just a setting? Any direction or help would be really appreciated!

    Thank you!

    opened by NateAGeek 1
Owner
David Tolnay
David Tolnay
Learn-rust-the-hard-way - "Learn C The Hard Way" by Zed Shaw Converted to Rust

Learn Rust The Hard Way This is an implementation of Zed Shaw's Learn X The Hard Way for the Rust Programming Language. Installing Rust TODO: Instruct

Ryan Levick 309 Dec 8, 2022
Simple and customizable procedural noise generation library written in Rust.

libnoise A simple, performant, and customizable procedural noise generation library inspired by libnoise for C++ featuring: Easy coherent noise genera

SpoogieOogie 29 Aug 13, 2023
A procedural macro for configuring constant values across crates

toml-cfg Rough ideas: Crates can declare variables that can be overridden Anything const, e.g. usize, strings, etc. (Only) The "root crate" can overri

James Munns 43 Dec 24, 2022
Procedural macro to derive Serde serializer-deserializer for Prost

prost-serde-derive prost-serde-derive is a procedural macro to generate Serde serializers and deserializers for Prost-generated structs. Rationale Cur

Park Joon-Kyu 4 Dec 15, 2022
A procedural macro to generate a new function implementation for your struct.

Impl New ?? A procedural macro to generate a new function implementation for your struct. ?? Add to your project Add this to your Cargo.toml: [depende

Mohammed Alotaibi 4 Sep 8, 2023
Simple autoclicker written in Rust, to learn the Rust language.

RClicker is an autoclicker written in Rust, written to learn more about the Rust programming language. RClicker was was written by me to learn more ab

null 7 Nov 15, 2022
A repository for showcasing my knowledge of the Rust programming language, and continuing to learn the language.

Learning Rust I started learning the Rust programming language before using GitHub, but increased its usage afterwards. I have found it to be a fast a

Sean P. Myrick V19.1.7.2 2 Nov 8, 2022
Try to learn Rust in a week. The goal is to finish a quiz at the end of the week.

RustInAWeek Try to learn Rust in a week. The goal is to finish the quiz at the end of the week. Quiz link https://dtolnay.github.io/rust-quiz/1 Book l

null 1 Dec 13, 2021
LearnRustTogether - Let's learn Rust together

Curated collection of lists of useful resources to learn Rust together. List of forums and chats you may find here. I encourage you to seek for help i

Learn Together 3 Nov 29, 2022
Learn Rust by writing Entirely Too Many linked lists

Learn Rust by writing Entirely Too Many Linked Lists Read the pretty version at https://rust-unofficial.github.io/too-many-lists/. Building Building r

null 2.4k Jan 3, 2023
Learn programming with Rust as a first language (book)

Learn programming with Rust as first language This is a book to learn programming from scratch. Read the book here: https://deavid.github.io/lprfl/ LI

David Martínez Martí 2 May 21, 2022
This Repo Contains my Week Long Journey Trying to Learn Rust Programming Language 🦀.

the-rust-way This Repo Contains my Week Long Journey Trying to Learn Rust Programming Language ?? . ?? Thanks to all Wonderful Contributors Thanks a l

Kanishk Pachauri 7 Oct 20, 2022
A console viewer for trees – pet project to help me learn Rust.

treeviewer This is a pet project to help me learn Rust. But maybe it’ll end up being of actual use for someone? The idea is to write a program that, g

Daniel Janus 3 Jul 15, 2023
Repository to learn fractal generation algorithms.

Fractalrs Created this project so I can study Rust while programming fractals! I have always wanted to learn fractal generation. Fractals Fractals are

Luke Dias 4 Jun 10, 2023
Lightweight parsing for Rust proc macros

Lightweight parsing for Rust proc macros Venial is a WIP parser for Rust proc macros. When writing proc macros that need to parse Rust code (such as a

Olivier FAURE 148 Dec 30, 2022
A Rust crate providing utility functions and macros.

介绍 此库提供四类功能:异常处理、http post收发对象、格式转换、语法糖。 在 Cargo.toml 里添加如下依赖项 [dependencies.xuanmi_base_support] git = "https://github.com/taiyi-research-institute/x

null 17 Mar 22, 2023
Attribute for defining `macro_rules!` macros with proper visibility and scoping

macro-vis This crate provides an attribute for defining macro_rules! macros that have proper visibility and scoping. The default scoping and publicity

null 2 Aug 29, 2022
Linux daemon to bind keys and macros to your controller's buttons

makima Makima is a daemon for Linux to bind your controller's buttons to key sequences and macros. Features: Configure your keybindings through a simp

null 48 Jun 14, 2023
Proc-macros for generating icons from the Iconify API

iconify-rs This crate provides a macro to embed SVGs from Iconify. For a list of icons, see Iconify Icon Sets. ?? Usage let svg = iconify::svg!("mdi:h

Matthew Taylor 5 Jul 9, 2023