Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 'practical macros' section to use '=>' instead of '...' syntax… #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

strangedreamsNY
Copy link

Edited this chapter slightly to ensure as much code as possible is valid throughout or where it is not, ensure that any errors are 'obvious' and not distracting of the chapters intent. Specifically, I updated the example code to use => instead of ... in the macro pattern match. The later value is not actually usable without additional delimiters and results in misleading errors in code that references the macro. This can lead a reader to think they don't correctly understand the parsing structure and clarification doesn't happen until much later in the chapter, rendering all of the previous code examples effectively unusable if they're (likely) following along.

… in the provided examples. This hopefully alleviates confusion that can stem from using the current example code and seeing errors related to the tokens instead of an expected error related to unimplemented code
@@ -32,20 +32,23 @@ So, with all that having been said, let's get started.
## Construction

Usually, when working on a new `macro_rules!` macro, the first thing I do is decide what the invocation should look like.
In this specific case, my first attempt looked like this:
In this specific case, my second* attempt looked like this:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rather confusing wording, makes the reader question about the first attempt

Comment on lines -585 to +569
# if self.pos < 2 {
# let next_val = self.mem[self.pos];
# self.pos += 1;
# Some(next_val)
# } else {
# let next_val = {
# let n = self.pos;
# let a = IndexOffset { slice: &self.mem, offset: n };
# (a[n-2] + a[n-1])
# };
#
# {
# use std::mem::swap;
#
# let mut swap_tmp = next_val;
# for i in (0..2).rev() {
# swap(&mut swap_tmp, &mut self.mem[i]);
# }
# }
#
# self.pos += 1;
# Some(next_val)
# }
if self.pos < 2 {
let next_val = self.mem[self.pos];
self.pos += 1;
Some(next_val)
} else {
let next_val = {
let n = self.pos;
let a = IndexOffset { slice: &self.mem, offset: n };
(a[n-2] + a[n-1])
};
{
use std::mem::swap;
let mut swap_tmp = next_val;
for i in (0..2).rev() {
swap(&mut swap_tmp, &mut self.mem[i]);
}
}
self.pos += 1;
Some(next_val)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentionally hidden, its noise regarding the change described

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants