pub struct LitStr { /* private fields */ }
Expand description
A UTF-8 string literal: "foo"
.
This type is available if Syn is built with the "derive"
or
"full"
feature.
Implementations
sourceimpl LitStr
impl LitStr
pub fn new(value: &str, span: Span) -> Self
pub fn value(&self) -> String
sourcepub fn parse<T: Parse>(&self) -> Result<T, Error>
pub fn parse<T: Parse>(&self) -> Result<T, Error>
Parse a syntax tree node from the content of this string literal.
All spans in the syntax tree will point to the span of this LitStr
.
Example
use proc_macro2::Span;
use syn::{Attribute, Ident, Lit, Meta, MetaNameValue, Path};
use syn::parse::{Error, Result};
// Parses the path from an attribute that looks like:
//
// #[path = "a::b::c"]
//
// or returns the path `Self` as a default if the attribute is not of
// that form.
fn get_path(attr: &Attribute) -> Result<Path> {
let default = || Path::from(Ident::new("Self", Span::call_site()));
let meta = match attr.interpret_meta() {
Some(meta) => meta,
None => return Ok(default()),
};
if meta.name() != "path" {
return Ok(default());
}
match meta {
Meta::NameValue(MetaNameValue { lit: Lit::Str(lit_str), .. }) => {
lit_str.parse()
}
_ => {
let error_span = attr.bracket_token.span;
let message = "expected #[path = \"...\"]";
Err(Error::new(error_span, message))
}
}
}
pub fn span(&self) -> Span
pub fn set_span(&mut self, span: Span)
Trait Implementations
sourceimpl ToTokens for LitStr
impl ToTokens for LitStr
sourcefn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
sourcefn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
impl Eq for LitStr
impl Token for LitStr
Auto Trait Implementations
impl RefUnwindSafe for LitStr
impl !Send for LitStr
impl !Sync for LitStr
impl Unpin for LitStr
impl UnwindSafe for LitStr
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Spanned for Twhere
T: ToTokens,
impl<T> Spanned for Twhere
T: ToTokens,
sourcefn span(&self) -> Span
fn span(&self) -> Span
Returns a
Span
covering the complete contents of this syntax tree
node, or Span::call_site()
if this node is empty. Read more