Expand description
This is a serde::Deserializer implementation for
proc_macro2::TokenStream. It is intended for proc_macro builders who
want rich configuration in their custom attributes.
If you’d like the consumers of your macro use it like this:
ⓘ
#[my_macro {
settings = {
reticulate_splines = true,
normalizing_power = false,
},
disaster = "pandemic",
}]Your macro will start like this:
ⓘ
#[proc_macro_attribute]
pub fn my_macro(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
// ...Use serde_tokenstream to deserialize attr into a structure with the
Deserialize trait (typically via a derive macro):
let config = match from_tokenstream::<Config>(&TokenStream::from(attr)) {
Ok(c) => c,
Err(err) => return err.to_compile_error().into(),
};§Nested attributes
For attributes that are nested inside a top-level macro, use the
from_tokenstream_spanned function. See its help for an example.
Structs§
- Ordered
Map - A container for pairs that are deserialized from map syntax if the keys are
non-unique, or don’t implement
HashorOrd. - Parse
Wrapper - A wrapper around
syn’sParsetrait that implementsDeserializein the context offrom_tokenstream. - Token
Stream Wrapper - A wrapper around
TokenStreamthat implementsDeserializein the context offrom_tokenstream.
Functions§
- from_
tokenstream - Deserialize an instance of type
Tfrom aTokenStream. - from_
tokenstream_ spanned - Deserialize an instance of type
Tfrom aTokenStreamwith data inside, along with aDelimSpanfor the surrounding braces.