Skip to content
ImagineWorld

Index

node_idnode-book_write-powerful-rust-macros_chapter-13-index
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num13
chapter_titleIndex
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph
  • abort_call_site 161
  • abort_call_site! macro 159
  • abort! macro 155, 157, 159, 161
  • additive features 248
  • ADTs (algebraic data types) 132
  • ARN (Amazon Resource Name) 224
  • as_ref method 118
  • AsRef trait 24
  • AsRefMut trait 24
  • AST (Abstract Syntax Tree) 46, 59
  • ast variable 62
  • async(keyword) 257
  • attr parameter 193
  • attribute macros 5, 60, 253–254
    • making fields public with 57, 60, 67–72
    • delegating tasks to custom struct 67–69
    • implementing Parse trait 69–71
    • using cursor 71
    • public fields, setup of project 58
  • attribute tokens and attributes 192–197
  • Attribute type 168
  • Attribute::parse_inner 198
  • Attribute::parse_outer 198
  • AttributeArgs struct 195
  • attributes
    • alternative naming for 170–173
    • builder pattern with 179
      • avoiding illegal states and type state pattern 180
      • avoiding scattered conditionals 190
      • better error messages for defaults 177–179
      • sensible defaults 173–177
    • with type state 182
    • getting and using fields 62–66
    • other kinds of 197
  • attributes property 192
  • attrs 266
    • defined on struct 62
  • AWS (Amazon Web Services), clients 225–229
  • AWS CDK 205
  • AWS Identity Access Management (IAM) 224
  • AWS SAM 205
  • black-box
    • tests 99
    • unit tests 105
  • block expression 16
  • Box 176
  • braced macro 198
  • builder macros
    • black-box unit tests 105
    • improvements 116
    • project setup 99
    • testing 97, 116, 119, 126
      • happy path test with actual property 107
      • structure of setup 101–102
      • types of tests 124
    • unhappy path testing 122
    • white-box unit tests 103–105
  • builder pattern
    • attribute tokens and attributes 192–197
    • rename attribute 164–170
    • implementing attribute behavior 165–168
    • parsing variations 169

INDEX

builder pattern
 rename attribute (continued)
  testing new attribute 164
 sensible defaults 173–177
 with attributes. See attributes
 with type state 182

C

call_site 102
call_site() function 84, 86
cargo
 Cargo.lock file 252
 Cargo.toml file 252
 check 47
 expand 51, 60, 72, 190
 init -lib command 43
 install cargo-expand 51
 run command 288
clean code 23
Clone functionality 42
clone method 116
closure 32, 170, 172, 176
CloudFormation 205
code, expanding 21
codegen_route function 273
collect 65, 92, 152–153, 176, 195, 265
colon_token 165
composing function-like macros 89–93
concat macro 121
configuration macro 236–241
 code overview 237–240
 project structure 236
 using full paths 240–241
contract testing 125
control flow 129
CreateFunctionOutput 286
cucumber testing framework 29
curry2 function 35
cursor 71
custom_keyword macro 210
cyclomatic complexity 129

D

Data enum, defined 62
Data struct 274
DataEnum 265
dbg! macro 4
Debug functionality 42
debugging function-like macros 88
declarative macros 4, 12, 229–231
 composing 31
 currying 34

DSLs 28
hygiene 36
lazy_static crate 36–38
newtypes 23
vectors 13–19
 declaring and exporting declarative
  macros 14
 matchers 15
 nonempty matchers 15–19
 syntax basics 14
Decode trait 7
default arguments 19
Default implementation 190
Default trait 173, 274
deref method 37
#[derive] annotation 42
derive macro 5, 42, 55
 helper attributes 165
#[derive(Builder)] attribute 164
#[derive(Debug)] 67
#[derive(Deserialize)] attribute 2, 253
#[derive(Hello)] attribute 47
DeriveInput 67, 138, 174, 242, 262
 import 244
 type 62
Diagnostic 162
doc profile 249
doc, adding reference to documentation 160
doctests 125
documentation
 adding to code exercise solutions 288
 blocks 197
domain-driven design 23
Dropper struct 74
DSLs (domain-specific languages) 9, 28, 42
 creating with function-like macros 78
 hiding information with function-like
  macros 78
 infrastructure
  alternative parsing approaches 215
  using Punctuated with custom struct 215
DTOs (Data Transfer Objects) 58

E

eager evaluation 172
EC2 (Elastic Compute Cloud) 205
emit_as_item_tokens() 162
emit_call_site_error! macro 159
emit_call_site_warning! macro 159
emit_error! macro 157–159
emit_warning! macro 159
end-to-end tests 124

  • entry::main function 254
  • Enum 265
  • eprintln command 72
  • eprintln macro 234
  • Err 151
    • type 173
    • value 135
  • error handling 128
    • alternatives to exceptions 132–134
    • deciding between syn and proc_macro_error 159
    • errors and control flow 129
    • flavors 149
    • overview of 155
    • panics 135–136
    • proc_macro_error crate for 155
    • pure and impure functions 130–132
    • real-world examples 159–162
    • replacing panics with results 140–145
    • Result type 135–136
    • setup of panic project 137
    • stopping panics 145–149
      • changing panic into Result 145–148
      • debugging observations 148
    • syn error handling 150
  • errors 161, 229–231
  • eval() function 4
  • EventBucket variant 283
  • exceptions, alternatives to 132–134
  • exchange macro 29
  • exclude keyword 246
  • exclude property 258, 288
  • expand command 52
  • expand_no_panic 76
  • expect function 110, 136
  • expected identifier error 243
  • expr expression 16
  • expr fragments 34
  • expr macro 259
  • expr variant 144, 230
  • expr, defined 39
  • Expr::If 277
  • Expr::While 277
  • extract_panic_content helper 146–147
  • features 244–248
  • fields 184
    • attribute macros, getting, using fields 62–66
    • making public with attribute macros 57, 60, 67, 72
    • delegating tasks to custom struct 67–69
  • implementing Parse trait 69–71
    • using cursor 71
  • FieldsNamed 274
  • Fn function 176
  • fn function 14, 176
  • Fold 258
  • forget call 75
  • format macro 52
  • format_args! macro 51
  • format_ident macro 95, 102, 280
  • format! macro 2, 151
  • fragment specifier 16
  • From implementation 39, 289
  • From macro 260
  • From trait 229
  • from_ast 279
  • FromIterator 153
  • full paths 240–241
  • function-like macros 6, 78
    • composing 89–93
    • creating mini-DSLs with 78
    • debugging by writing normal code 88
    • information hiding 79–88
      • generating helper methods 84–88
      • recreating struct 81–83
      • setup of information-hiding macro 79–81
    • overview 94
  • functional feature 245, 248
  • functions, pure and impure 130–132
  • fuzz testing 125
  • generics field 67
  • get call 73, 286
  • help, for checking signatures 161
  • hiding information with function-like macros 78
  • html macro 95
  • hygiene 36
    • for identifiers 35–36
  • IaC (Infrastructure as Code) 204
  • IAM (AWS Identity Access Management) 224
  • IDE (integrated development environment) 10
  • Ident 54, 216, 285
  • ident 39
  • ident identifier 16

INDEX

ident macro 259
ident_name function 52
identifier 49
if let 284
immutable returns 139
impl 176
impl block 87
impl Iterator 265
impure function 130
information hiding 79–88
 generating helper methods 84–88
 recreating struct 81–83
 setup of information-hiding macro 79–81
infrastructure DSL (domain-specific language) 203
 alternative parsing approaches 215
 AWS clients 225–229
 creating services 221–225
 errors and declarative macros 229–231
 implementing Parse trait for structs 209
 overview 205
 parsing input 207
 project setup and usage examples 207
 real-world example 231–234
 testing 231
 using Punctuated with custom enum and builder 218
inner attributes 197
integration tests 124
Into 138
Into trait 46, 81, 102
into_iter 266
is_ident method 166, 200
item parameter 59, 62
item, defined 17
ItemFn 138
ItemStruct 62
iter method 178

K

kw module 210

L

Lambda functions 205
lazy evaluation 172
lazy_static crate macro 36–38
Leptos 161
let statement 17
lib section 43
lib.rs file 43, 103, 174, 183
lifetime 17

lifetime elision rules 114
List variant 167–168
literal macro 259
literal, defined 17, 39
LitInt 211, 220
LitStr 169, 195, 211
load testing 125
location_constraint 228
log_syntax! macro 22
lookahead 279
loom 125

M

macro variable 16
macro_rules! 14, 26
macros 5, 235, 258
 adding 242
 appropriate use cases for 6–9
 attribute macros 66
  making fields public with 72
 attribute macros vs. derive macros 59
 composing 31
 configuration macro 236–241
  code overview 237–240
  project structure 236
  using full paths 240–241
 currying 34
 declarative 39
  lazy_static crate 36–38
  newtypes 23
  use cases 19
 default arguments 19
 documenting 248–250
 exercise solutions 259–262
 expanding code 21
 features 244–248
 from real world 253
 inappropriate use cases for 10
 publishing 250
 varargs 19
main functions 85, 151
main.rs file 20
@MAKE TY 37
map 65
marker trait 181
match 171, 264
matchers 12, 15–19
memcpy 122
memoization 131
meta 11, 17, 37, 167
meta enum 167
MetaList 167, 194

  • appropriate use cases for macros 6–9
  • defined 3
  • exercises 11
  • inappropriate use cases for macros 10
  • macros 5

methods, generating helper methods 84–88

Section titled “methods, generating helper methods 84–88”

  • name (identifier) of struct 62
  • NamedValue variant 167
  • naming conventions 201
  • nested property 196
  • new method 15, 24, 84, 96, 168, 236, 239, 241, 269–270
  • newtypes 23
  • next_field_struct_name 188
  • no rules expected the token :: 14
  • #[no_panic] macro 76
  • nonempty matchers 15–19
  • Normal variant 282–284
  • Not type 271
  • Nothing type 76
  • NullPointerException 130

  • object-oriented programming 31
  • Ok value 135, 153
  • once_cell 36
  • one-line doc comments 197
  • Option 83, 173
  • optional_default_asserts 280
  • original_struct_setters 186, 281
  • original_struct_setters method 174
  • orphan rules 27
  • outer attributes 197
  • output
    • field 142
    • generating 48–50
    • variable 284

  • panics 135–136
    • replacing with results 140–145
    • stopping 145–149
      • changing panic into Result 145–148
      • debugging observations 148
  • parenthesized macro 215
  • Parse 219, 267, 274
  • Parse implementation 74, 199
  • Parse input 91
  • parse method 69, 138, 210
  • Parse trait 73, 194
    • implementing 69–71
    • implementing for structs 209
  • parse_args method 168
  • parse_declaration 53
  • parse_macro_input 138, 193–194, 196
  • parse_macro_input function 49
  • parse_meta method 169
  • parse_nested_meta 196
  • parse_nested_meta method 169, 200
  • parse_number 221
  • parse_quote 75
  • parse_terminated 194
  • parse2 70, 76, 101, 104
  • PartialEq 104
  • partially applied function 31
  • paste! macro 232
  • pat_param, defined 17
  • pat, defined 17
  • path method 166
  • path property 194, 196, 258, 288
  • Path variant 167
  • path, defined 17
  • peek method 210
  • performance testing 125
  • PhantomData 181, 185
  • placeholder lifetime 114
  • point-free style 147
  • pollster crate 224
  • prelude 15
  • print macro 233
  • println macro 4, 51, 233–234
  • proc macro error 128
  • #[proc_macro_attribute] attribute 59
  • #[proc_macro_derive] attribute 59
  • proc_macro_error 160, 276
  • proc_macro_error crate, for error handling 155
  • proc_macro_errors 161
  • proc_macro2 101–102, 161, 219
  • proc_macro2 TokenStream 46
  • proc_macro2 wrapper 99

INDEX

proc_macro2_diagnostics 161
proc_macro2::TokenStream 102, 151
proc-macro = true 253
proc-macro property 43
proc-macro2 219, 253
proc-macro2 package 4
procedural macros 1
 analyzing setup 45–48
 basic setup of project 42–44
 experimenting with code 50
 generating output 48–50
 Hello, World 41
 using cargo expand 51
property-based testing 125
pub keyword, indicator of public visibility 37, 66
#[public] attribute 59
public fields 57
 attribute macros vs. derive macros 59
 with attribute macros 66–67, 72
  delegating tasks to custom struct 67–69
  implementing Parse trait 69–71
  using cursor 71
public fields macro 274
public visibility, making fields public with attribute macros 60
Punctuated 70, 194–195, 218, 266–268, 288
 using with custom struct 215
pure and impure functions 130–132
pure functions 124

Q

query macro 232
query method 94
quote 59, 61, 65–66, 102, 104, 151, 176, 243, 265–266
 + parse2 116
 declarative macro 65
 dependency 43, 58
 function 48
 library 99
 macro 46–47, 75, 81, 106, 110–111
 package 4
 spanned 177

R

README.md file 250
refactoring, enabling with testing 113
referential transparency 131
rename attribute 164–170
 implementing attribute behavior 165–168
 parsing variations 169

testing new attribute 164
require_list method 167
require_name_value method 167
require_path_only method 167
Result type 101, 135–136, 153, 173, 218, 281
return value 144
returns
 immutable 139
 mutability 139
Rocket 161
rt-multi-thread flag 224
Runtime high-level concurrency structures 256–257
runtime overhead 28
RUST_IDE_PROC_MACRO_COMPLETION environment variable 222
rustc_ast 54
rustc_expand 54
rustc_span 54

S

S3 (Simple Storage Service) 205
s3:ObjectCreated 228
S3Client 227
scattered conditionals 190
SDKs (software development kits) 205
self 188
Semi variant 144
semver (semantic versioning) 252
serde package 4
serde_test 126
#[serde(rename = “name”)] 201
ServiceError type 230
services, creating 221–225
Shuttle 160, 162, 203, 231–232
side effects 130
sig property 141
sig.output 142
signature 144
signature_output_as_result 151
smoke tests 124
Span 54, 84
span 161, 218
sqlx package 7
SQS (Simple Queue Service) 205
src folder 51
static ref 37
static values 36
stmt 17
stmts (statements) 76
stmts vector 143
strategy pattern 190

INDEX

String pair 26
String type 281
stringify macro 55, 121, 263–264
struct feature 244, 248
struct_name 185
structs
 custom, using Punctuated with 215
 recreating 81–83
 white-box testing for 272–274
sum type 133
syn 268
 crate 24, 49
 dependency 43
 error handling 150
 errors 160, 201, 222
 feature 138
 feature flags 258
 package 4
 struct 155
 syn::Error 128, 151, 153, 162, 219, 275
 syn::Error::new 159
 syn::Error::new_spanned 159
 syn::ItemStruct construct 62

T
@TAIL 37
TDD (test-driven development) 99
template macro 253
#[test] attribute 2
test macro 199
testing
 builder macros 119, 126
  happy path test with actual property 107
 enabling refactoring 113
 happy path test 105
 infrastructure DSL 231
testing_testing function 55, 263
tests
 happy path test with actual property 107
 structure of setup 101–102
 types of tests 124
 unhappy path testing 122
 white-box unit tests 103–105
tests directory 237
tests folder 105, 126, 183
thiserror crate 231
to_string calls 241
to_string method 118
to_token_stream 138
to_tokens 104
Token macro 91, 96, 271
tokens 168

TokenStream 54, 58–59, 76, 80, 102, 104, 128, 138, 168, 265, 274
 attribute 242
 type 45
 with the public prefix 65
TokenTree 16–17, 52
#[tokio::main] macro 73, 223
ToTokens 159
 implementation 93, 243
 trait 73, 81
transcribers 12
traverse 153
trybuild 126, 177
 crate 122
 dependency 236
TryFrom 39
TryFrom macro 260
tt (TokenTree) 16–17, 39
tt macro 259
ty (Type) 26, 39
ty macro 259
ty type 17
ty variant 230
type state 182
type state pattern 180

U
u16 213, 220
unexpected token error 199
unhappy path testing 122
unimplemented 162, 276
 arm 67
 match 67
 panic 160
 variant 136
Unions 67
unit tests, types of 123
unwrap 104
unwrap function 136
unwrap_or_default() method 175
unwrap_or_else 170, 172
update_function_configuration() function 286
usage directory 252
use one-liner 47
util/create_setup.sh script 44

V
value method 195
varargs 19
variants 265–266
Vec 176, 178, 281