Skip to main content

SymbolId

Struct SymbolId 

Source
pub struct SymbolId(/* private fields */);
Expand description

The canonical identity of a symbol. See the module docs for the grammar.

An id holds its canonical text (shared, so cloning is cheap). Because printing is injective and only canonical text parses, two ids are equal exactly when their texts are, and ids order by their text. A parent’s text is a prefix of its children’s, so a parent always sorts before them. Structural accessors (descriptors, package, …) read the text on demand.

Implementations§

Source§

impl SymbolId

Source

pub fn global(package: Package, descriptors: Vec<Descriptor>) -> Self

A global id: package followed by descriptors (empty for the package root).

Source

pub fn package_root(package: Package) -> Self

The package root (a crate’s root module).

Source

pub fn path<I, S>(segments: I) -> Result<Self, IdError>
where I: IntoIterator<Item = S>, S: Into<String>,

A path as written, for references whose kinds are unknown.

Source

pub fn unresolved(name: impl Into<String>) -> Self

A method called on a receiver of unknown type.

Source

pub fn parse(text: &str) -> Result<Self, IdError>

Parse the canonical form. The exact inverse of Display: only canonical text is accepted (a simple name in backticks, for instance, is rejected), so parse(s) succeeding implies parse(s)?.to_string() == s.

Source

pub fn as_str(&self) -> &str

The canonical text, sym: prefix included.

Source

pub fn is_global(&self) -> bool

true for global (kind-explicit) ids.

Source

pub fn is_unresolved(&self) -> bool

true for SymbolId::unresolved ids.

Source

pub fn package(&self) -> Option<Package>

The package of a global id.

Source

pub fn descriptors(&self) -> Vec<Descriptor>

The descriptors of a global id (empty for the other forms).

Source

pub fn segments(&self) -> Option<Vec<String>>

The segments of a path id.

Source

pub fn last(&self) -> Option<Descriptor>

The last descriptor of a global id.

Source

pub fn name(&self) -> Cow<'_, str>

The short name: the last descriptor’s name, the package name of a package root, the last path segment, or the unresolved method name.

assert_eq!(SymbolId::parse("sym:cargo a . b/C#go().").unwrap().name(), "go");
assert_eq!(SymbolId::parse("sym:cargo a .").unwrap().name(), "a");
Source

pub fn root(&self) -> Option<Cow<'_, str>>

The first name: the package name, or the first path segment. None for unresolved ids.

Source

pub fn parent(&self) -> Option<SymbolId>

The enclosing definition. For a global id, the last descriptor is dropped, then any trailing type-parameter descriptors, so a method in a trait-impl block has its type as parent. For a path id, the last segment is dropped. A package root, a one-segment path and an unresolved id have no parent.

let m = SymbolId::parse("sym:cargo a . T#[Tr]f().").unwrap();
assert_eq!(m.parent().unwrap().to_string(), "sym:cargo a . T#");
assert_eq!(SymbolId::parse("sym:cargo a . T#").unwrap().parent().unwrap().to_string(), "sym:cargo a .");
assert!(SymbolId::parse("sym:cargo a .").unwrap().parent().is_none());
Source

pub fn child(&self, descriptor: Descriptor) -> Option<SymbolId>

Append a descriptor to a global id. None for the other forms, which have no kinds to extend.

Source

pub fn names(&self) -> Vec<Cow<'_, str>>

Every name in order: package and descriptor names for a global id, the segments of a path id, the name of an unresolved id.

Source

pub fn extend_path(&self, segment: &str) -> SymbolId

Continue an id with a segment of unknown kind: the result is a path id made of names plus segment. This is how a reference that leaves known territory (krate::known::unknown) is recorded.

let m = SymbolId::parse("sym:cargo a . net/").unwrap();
assert_eq!(m.extend_path("dial").to_string(), "sym:extern a::net::dial");
Source

pub fn view(&self) -> IdView<'_>

A borrowed view of the id’s parts. Unlike package and descriptors it allocates nothing but the descriptor list (and a name only when it needs unescaping), so it suits hot paths such as deriving a diagram id per call site.

use sealmap_model::{DescriptorKind, IdView, SymbolId};

let id = SymbolId::parse("sym:cargo shop . db/Db#[Store]put().").unwrap();
let IdView::Global(g) = id.view() else { unreachable!() };
assert_eq!((g.manager, &*g.package, g.version.is_none()), ("cargo", "shop", true));
let kinds: Vec<_> = g.descriptors.iter().map(|d| (d.kind, &*d.name)).collect();
assert_eq!(kinds, [
    (DescriptorKind::Namespace, "db"),
    (DescriptorKind::Type, "Db"),
    (DescriptorKind::TypeParameter, "Store"),
    (DescriptorKind::Method, "put"),
]);
Source

pub fn display_path(&self) -> String

The names joined by ::, for human-facing labels. Not injective: never use it as a key.

Trait Implementations§

Source§

impl Clone for SymbolId

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SymbolId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SymbolId

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for SymbolId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for SymbolId

Source§

impl FromStr for SymbolId

Source§

type Err = IdError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for SymbolId

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for SymbolId

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for SymbolId

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for SymbolId

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for SymbolId

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for SymbolId

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.