Struct Path

pub struct Path { pub leading_colon: Option<PathSep>, pub segments: Punctuated<PathSegment, PathSep> }

A path at which a named item is exported (e.g. alloc::collections::HashMap).

Fields

leading_colon: Option<PathSep>
segments: Punctuated<PathSegment, PathSep>

Implementations

impl Path

fn parse_mod_style(input: ParseStream<'_>) -> Result<Self>

Parse a Path containing no path arguments on any of its segments.

Example

use syn::{Path, Result, Token};
use syn::parse::{Parse, ParseStream};

// A simplified single `use` statement like:
//
//     use alloc::collections::HashMap;
//
// Note that generic parameters are not allowed in a `use` statement
// so the following must not be accepted.
//
//     use a::<b>::c;
struct SingleUse {
    use_token: Token![use],
    path: Path,
}

impl Parse for SingleUse {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(SingleUse {
            use_token: input.parse()?,
            path: input.call(Path::parse_mod_style)?,
        })
    }
}

impl Path

fn is_ident<I>(&self, ident: &I) -> bool
where
    I: ?Sized,
    Ident: PartialEq<I>,

Determines whether this is a path of length 1 equal to the given ident.

For them to compare equal, it must be the case that:

  • the path has no leading colon,
  • the number of path segments is 1,
  • the first path segment has no angle bracketed or parenthesized path arguments, and
  • the ident of the first path segment is equal to the given one.

Example

use proc_macro2::TokenStream;
use syn::{Attribute, Error, Meta, Result};

fn get_serde_meta_item(attr: &Attribute) -> Result<Option<&TokenStream>> {
    if attr.path().is_ident("serde") {
        match &attr.meta {
            Meta::List(meta) => Ok(Some(&meta.tokens)),
            bad => Err(Error::new_spanned(bad, "unrecognized attribute")),
        }
    } else {
        Ok(None)
    }
}
fn get_ident(&self) -> Option<&Ident>

If this path consists of a single ident, returns the ident.

A path is considered an ident if:

  • the path has no leading colon,
  • the number of path segments is 1, and
  • the first path segment has no angle bracketed or parenthesized path arguments.
fn require_ident(&self) -> Result<&Ident>

An error if this path is not a single ident, as defined in get_ident.

Trait Implementations

impl Clone for Path

fn clone(&self) -> Self

impl Parse for Path

fn parse(input: ParseStream<'_>) -> Result<Self>

impl ToTokens for Path

fn to_tokens(&self, tokens: &mut TokenStream)

impl<T> From<T> for Path where T: Into<PathSegment>,

fn from(segment: T) -> Self

Auto Trait Implementations

impl !Send for Path

impl !Sync for Path

impl Freeze for Path

impl RefUnwindSafe for Path

impl Unpin for Path

impl UnsafeUnpin for Path

impl UnwindSafe for Path

Blanket Implementations

impl<T> Any for Path where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Path where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Path where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Path where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Path

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Spanned for Path where T: Spanned + ?Sized,

fn span(&self) -> Span

impl<T> ToOwned for Path where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Path where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Path where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Path where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>