pub trait ParseBufferExt: Sealed {
// Required methods
fn parse_bool_attr(&self) -> Result<bool, Error>;
fn parse_valued_attr<P>(&self) -> Result<P, Error>
where P: Parse;
fn iter_delimited<T, D>(&self) -> DelimitedIter<'_, T, D> ⓘ
where T: Parse,
D: Parse;
}
Available on crate feature
attr_parse
only.Expand description
ParseBuffer
extensions
Required Methods§
Sourcefn parse_bool_attr(&self) -> Result<bool, Error>
fn parse_bool_attr(&self) -> Result<bool, Error>
Parse a boolean attribute
Value | Result |
---|---|
my_attr | true |
my_attr(true) | true |
my_attr(false) | false |
my_attr = true | true |
my_attr = false | false |
§Examples
struct MyStruct(bool);
impl Parse for MyStruct {
fn parse(input: ParseStream) -> syn::Result<Self> {
input.parse_bool_attr().map(Self)
}
}
fn check(expect: bool, tokens: TokenStream) {
let tokens_str = tokens.to_string();
match syn::parse2::<MyStruct>(tokens) {
Ok(MyStruct(actual)) => assert_eq!(actual, expect, "parsed value mismatch"),
Err(e) => panic!("Error parsing `{tokens_str}`: {e}")
}
}
check(true, TokenStream::new());
check(true, quote!((true)));
check(false, quote!((false)));
check(true, quote!(= true));
check(false, quote!(= false));
Sourcefn parse_valued_attr<P>(&self) -> Result<P, Error>where
P: Parse,
fn parse_valued_attr<P>(&self) -> Result<P, Error>where
P: Parse,
Parse a valued attribute
Value | Result |
---|---|
my_attr(something) | P::parse(something) |
my_attr = something | P::parse(something) |
The my_attr(something)
syntax should be preferred as my_attr = something
can’t always
deserialise some types (e.g. Visibility
).
§Example
struct MyStruct(Ident);
impl Parse for MyStruct {
fn parse(input: ParseStream) -> syn::Result<Self> {
input.parse_valued_attr().map(Self)
}
}
let v: MyStruct = parse_quote!((foo));
assert_eq!(v.0, "foo");
let v: MyStruct = parse_quote!(= bar);
assert_eq!(v.0, "bar");
Sourcefn iter_delimited<T, D>(&self) -> DelimitedIter<'_, T, D> ⓘ
fn iter_delimited<T, D>(&self) -> DelimitedIter<'_, T, D> ⓘ
Shortcut for DelimitedIter::new
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.