pub struct PathSegment<Kind = AnyKind> { /* private fields */ }Expand description
A typed path segment: one “piece” of a Rust path, with extra structure.
§What does that mean?
In Rust, every item (function, type, trait, field…) has a path starting at
its crate root. For example, in a crate called my_crate:
mod a {
mod b {
fn hello() {}
}
trait Foo {
fn f() {
enum T {
C { field: u8 },
}
}
}
}Some paths and their segments are:
| Path | Segments |
|---|---|
my_crate | [my_crate] |
my_crate::a | [my_crate], [a] |
my_crate::a::b::hello | [my_crate], [a], [b], [hello] |
my_crate::a::Foo | [my_crate], [a], [Foo] |
my_crate::a::Foo::f | [my_crate], [a], [Foo::f] |
my_crate::a::Foo::f::T | [my_crate], [a], [Foo::f], [T] |
my_crate::a::Foo::f::T::C | [my_crate], [a], [Foo::f], [T::C] |
my_crate::a::Foo::f::T::C::field | [my_crate], [a], [Foo::f], [T::C::field] |
Each [X] here is a path segment.
§Hierarchy
Path segments form a hierarchy: each one knows its parent. For example, the
field my_field is inside the constructor of MyVariant, which is inside
the enum MyEnum, which lives inside the function f, and so on – all the
way up to the crate root.
This parenthood is important:
- a field segment always has a constructor parent
(e.g.
my_field → MyVariant). - an associated item always has a trait/impl container parent
(e.g.
f → Foo). - everything ultimately has a crate as its top parent.
§Why does this matter?
This strong typing of segments lets tools:
- disambiguate names across contexts (e.g. two types with the same constructor name),
- generate unique, human-readable names in other languages/backends,
- walk up the chain of parents to reconstruct full paths.
For example, with the F* backend, constructors are not namespaced under the
name of their type, but live directly at top-level. Thus, they need to be
unique. Using the hierarchy, we can print them as Foo_MyVariant instead of
Foo.MyVariant.
Implementations§
Source§impl<K> PathSegment<K>
impl<K> PathSegment<K>
Sourcepub fn payload(&self) -> PathSegmentPayload
pub fn payload(&self) -> PathSegmentPayload
Returns the payload of this path segment (named vs. unnamed and why).
Sourcepub fn disambiguator(&self) -> u32
pub fn disambiguator(&self) -> u32
Returns the rustc path disambiguator for this segment.
Source§impl PathSegment<ConstructorKind>
impl PathSegment<ConstructorKind>
Sourcepub fn lift(&self) -> PathSegment<AnyKind>
pub fn lift(&self) -> PathSegment<AnyKind>
Lift a PathSegment of kind ConstructorKind to a PathSegment of kind AnyKind.
Source§impl PathSegment<TypeDefKind>
impl PathSegment<TypeDefKind>
Sourcepub fn lift(&self) -> PathSegment<AnyKind>
pub fn lift(&self) -> PathSegment<AnyKind>
Lift a PathSegment of kind TypeDefKind to a PathSegment of kind AnyKind.
Source§impl PathSegment<AssocItemContainerKind>
impl PathSegment<AssocItemContainerKind>
Sourcepub fn lift(&self) -> PathSegment<AnyKind>
pub fn lift(&self) -> PathSegment<AnyKind>
Lift a PathSegment of kind AssocItemContainerKind to a PathSegment of kind AnyKind.
Source§impl PathSegment
impl PathSegment
Sourcepub fn parent(&self) -> Option<PathSegment>
pub fn parent(&self) -> Option<PathSegment>
Returns the parent path segment, if any.
Parents exist only for:
AnyKind::Constructor(parent is itsTypeDefKind),AnyKind::AssocItem(parent is its containertrait/impl),AnyKind::Field(parent is its constructor).
All other kinds return None.
Sourcepub fn parents(&self) -> impl Iterator<Item = Self>
pub fn parents(&self) -> impl Iterator<Item = Self>
Returns an iterator over self and all its ancestors, walking up via
Self::parent until no parent remains.
Trait Implementations§
Source§impl<Kind: Clone> Clone for PathSegment<Kind>
impl<Kind: Clone> Clone for PathSegment<Kind>
Source§fn clone(&self) -> PathSegment<Kind>
fn clone(&self) -> PathSegment<Kind>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<Kind: Debug> Debug for PathSegment<Kind>
impl<Kind: Debug> Debug for PathSegment<Kind>
Auto Trait Implementations§
impl<Kind> Freeze for PathSegment<Kind>where
Kind: Freeze,
impl<Kind> RefUnwindSafe for PathSegment<Kind>where
Kind: RefUnwindSafe,
impl<Kind> Send for PathSegment<Kind>where
Kind: Send,
impl<Kind> Sync for PathSegment<Kind>where
Kind: Sync,
impl<Kind> Unpin for PathSegment<Kind>where
Kind: Unpin,
impl<Kind> UnsafeUnpin for PathSegment<Kind>where
Kind: UnsafeUnpin,
impl<Kind> UnwindSafe for PathSegment<Kind>where
Kind: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more