Have some Confusion about Macro

macro readable_writeable($type:ident, $self:ident.$($impl:tt)+) {
    impl<T> ReadableWriteable<T> for $type<T>
        where T: ::core::ops::BitAnd<Output = T>, T: ::core::ops::BitOr<Output = T> { }

I don’t know how this Macro works, Especially that $impl:tt part. :disappointed_relieved:


I managed to find your code snippet in Stanford CS140e course skeleton code repository, specifically in volatile/src/lib.rs line 72.

The following assumes you’re taking the course. If you’re not, it’s still better to avoid making homework solutions available online to students.

I’d like to kindly remind that universities take academic honesty very seriously, and while your phrasing of the question was appropriate, it is best to state explicitly that you are asking something related to your homework, and also show the context via linking to whatever public materials.

When you ask questions regarding your homework, it is very important that you ask very carefully to make sure everything is in the clear. And letting others know this is related to a homework will help others to craft their responses more carefully, in a more useful way, and also avoid getting anyone in trouble due to academic dishonesty.

Providing context also helps you to get better replies, many people may choose to ignore your question immediately since you’re asking a very bespoke piece of code with possibly no common usage associated to the pattern.

I’d like to add that professors and tutors are very well aware of most(if not all) of the avenues where students may be able to ask homework questions, you’d be wrong if you think you would not get caught should you commit such acts. Incidents of that nature have occurred on the OCaml forum before.

It is your duty to uphold your academic honesty as you’re participating in the academia, and honesty in your career in general, and it is important that you make everything as explicit as you can when you are treading potentially sensitive issues.

Lastly, a somewhat similar question is present on the linked webpage already as follows

“What do the readable!, writeable!, and readable_writeable! macros do?”

Admittedly this is different from what you’re asking, and admittedly I have no idea if the questions are marked in your course, but people generally ask the more general case instead of a particular one, or craft a separate example, just to stay very clear.


Now to answer your question.

This syntax is experimental, if you compile it on the Rust playground, or a blank project, you will be linked to this issue.

Thus you may not find many useful information on it yet. Still, AFAIK it shares most components with existing macro construction mechanisms, namely macro_rules!, so the corresponding chapters in the Rust book should help you:

The skeleton code also contains two usages on volatile/src/lib.rs on line 137:

readable_writeable!(Volatile, self.0);

and line 164

readable_writeable!(UniqueVolatile, self.as_ptr());

If you look at it, you should be able to see it’s a rather straightforward pattern matching, so you should be able to answer the questions yourself.

As for why the macro is designed that way, I have no idea. You’d be better off asking the more specific questions on your universitiy’s forum or email your professor directly.


As a side note, I was able to find the code snippet and also the usage of the macro fairly quickly via a tool called ripgrep

[darren@darren_laptop ~]$ git clone https://web.stanford.edu/class/cs140e/assignments/1-shell/skeleton.git
Cloning into 'skeleton'...
[darren@darren_laptop ~]$ cd skeleton/
[darren@darren_laptop skeleton]$ rg "readable_writeable"
72:macro readable_writeable($type:ident, $self:ident.$($impl:tt)+) {
137:readable_writeable!(Volatile, self.0);
164:readable_writeable!(UniqueVolatile, self.as_ptr());

You can install it via cargo install ripgrep, and invoke it with rg.

I think you will find it very useful in your career especially when navigating very large code bases.