Created
June 25, 2019 15:03
-
-
Save rust-play/764863f77bd7fd615883d87ff002ec34 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! doc_comment { | |
($x:expr) => { | |
#[doc = $x] | |
extern {} | |
}; | |
// enum like types | |
($x:literal, $visibility:vis enum $ty_name:ident { | |
$($y:literal, $field:ident $(($($field_ty:ty),*))? ),* $(,)? | |
}) => { | |
#[doc = $x] | |
$visibility enum $ty_name { | |
$(#[doc = $y] $field $(($($field_ty),*))?,)* | |
} | |
}; | |
($visibility:vis enum $ty_name:ident { | |
$($y:literal, $field:ident $(($($field_ty:ty),*))? ),* $(,)? | |
}) => { | |
$visibility enum $ty_name { | |
$(#[doc = $y] $field $(($($field_ty),*))?,)* | |
} | |
}; | |
// struct like types | |
($x:expr, $visibility:vis struct $ty_name:ident {$($($meta:meta)? $y:expr, $visibility_i:vis $field:ident: $typ:ty $(,)?)*}) => { | |
#[doc = $x] | |
$visibility struct $ty_name { | |
$(#[doc = $y] $($meta)? $visibility_i $field: $typ,)* | |
} | |
}; | |
// struct like types without doc comment on the item | |
($visibility:vis struct $ty_name:ident {$($($meta:meta)? $y:expr, $visibility_i:vis $field:ident: $typ:ty $(,)?)*}) => { | |
$visibility struct $ty_name { | |
$(#[doc = $y] $($meta)? $visibility_i $field: $typ,)* | |
} | |
}; | |
($x:expr, $($tt:tt)*) => { | |
#[doc = $x] | |
$($tt)* | |
}; | |
} | |
doc_comment!( | |
pub struct Bar { | |
"field", // <- the comma here is mandatory | |
pub field: usize, | |
"hidden field", // <- still mandatory | |
private_field: usize, | |
} | |
); | |
// Of course, you do it while documenting the type as well: | |
doc_comment!( | |
"This is an enum!", // <- comma is mandatory | |
enum Enum { | |
"first variant", // <- the comma here is mandatory | |
Variant(usize), | |
"second variant", // still mandatory | |
SecondVariant, | |
} | |
); | |
fn main() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment