Skip to main content

Name

Struct Name 

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

A hierarchical name.

Names identify constants, inductives, and other declarations. They form a tree: Nat.add.comm is Str(Str(Str(Anonymous, "Nat"), "add"), "comm").

Structural sharing (wave5 Stage F): Name is a newtype over a reference-counted cell holding its outermost component (NameKind), so cloning a whole name — e.g. the name held by every Const referencing a constant — is an O(1) refcount bump, not a deep copy of the component chain. The parent link inside NameKind is itself a Name, so a name shares its entire prefix with any longer name built on it: at Mathlib scale (millions of recurring names and prefixes) residency is proportional to the number of distinct names, not to how many times each is referenced.

Each name also stores a hash of its own structure, computed when it is built from its parent’s stored hash and its last component (see the kernel-private structural_hash module) — so an Expr that mentions a name (a Const, a binder, a projection) folds it into its own hash in O(1), without walking the component chain.

Equality is exactly structural: the same allocation is equal, different stored hashes are different (equal names always hash equal), and otherwise the components are compared. Hash writes the components as it always did (not the stored hash), so a HashMap<Name, _> behaves as before.

Match on the outermost component through Name::view: a match name.view() { NameView::Str(parent, s) => .. } reads like the former match name { Name::Str(parent, s) => .. }.

Implementations§

Source§

impl Name

Source

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

Borrow this name’s outermost component for matching. O(1).

Source

pub fn anonymous() -> Name

The anonymous (root) name.

Source

pub fn mk_str(parent: Name, s: impl Into<String>) -> Name

Construct parent.s (string component). O(1) in the length of the name — shares parent and reads its stored hash.

Source

pub fn mk_num(parent: Name, n: u64) -> Name

Construct parent.n (numeric component). O(1) — shares parent and reads its stored hash.

Source

pub fn str(s: impl Into<String>) -> Self

Create a simple string name (no parent).

Source

pub fn append_str(self, s: impl Into<String>) -> Self

Append a string component to this name.

Source

pub fn append_num(self, n: u64) -> Self

Append a numeric component to this name.

Source

pub fn is_anonymous(&self) -> bool

Check if this is the anonymous name.

Source

pub fn from_str(s: &str) -> Self

Create a Name from a dot-separated string.

Name::from_str("Nat.add.comm") produces the same as Name::str("Nat").append_str("add").append_str("comm").

Source

pub fn depth(&self) -> usize

Returns the depth (number of components) of this name.

Anonymous has depth 0, Name::str("Nat") has depth 1, Name::str("Nat").append_str("add") has depth 2.

Source

pub fn last_str(&self) -> Option<&str>

Return the last string component of this name, if any.

For Nat.add, returns Some("add").

Source

pub fn last_num(&self) -> Option<u64>

Return the last numeric component, if any.

Source

pub fn root(&self) -> Option<&str>

Return the root (top-level) component as a string.

For Nat.add.comm, returns "Nat".

Source

pub fn prefix(&self) -> Name

Return the parent name (prefix with last component removed).

Source

pub fn has_prefix(&self, prefix: &Name) -> bool

Check whether this name has prefix as a (strict) prefix.

Nat.add.comm.has_prefix(Nat) is true.

Source

pub fn components(&self) -> Vec<String>

Collect all components from root to leaf.

Returns a vector of (is_num, string_or_num) pairs.

Source

pub fn from_components(comps: &[String]) -> Self

Reconstruct a Name from a list of string components.

Numeric strings are converted to Num components.

Source

pub fn replace_last(self, new_last: impl Into<String>) -> Self

Replace the last string component with new_last.

If the name ends in a numeric component, appends new_last instead.

Source

pub fn freshen(self, suffix: u64) -> Self

Produce a “fresh” version of this name by appending a suffix number.

Used to avoid name collisions during elaboration.

Source

pub fn in_namespace(&self, ns: &Name) -> bool

Check whether this name is in the given namespace.

A name is in namespace ns if it has ns as a strict prefix.

Source

pub fn with_suffix(self, suffix: impl Into<String>) -> Self

Append a suffix string to this name.

Source

pub fn to_ident_string(&self) -> String

Convert to a string suitable for use as a Rust identifier.

Dots and special characters are replaced with underscores.

Source

pub fn prepend(self, prefix: Name) -> Self

Return a new name with the given prefix prepended.

Source

pub fn is_str_name(&self) -> bool

Check whether this name is a string name (last component is a string).

Source

pub fn is_num_name(&self) -> bool

Check whether this name is a numeric name (last component is a number).

Trait Implementations§

Source§

impl Clone for Name

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 Name

Source§

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

Formats as Name(<kind>) — the output of the derived implementation when Name wrapped the component kind directly. Code that hashes or orders terms by their Debug text depends on this spelling.

Source§

impl Default for Name

Source§

fn default() -> Self

The anonymous name.

Source§

impl Display for Name

Source§

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

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

impl Eq for Name

Source§

impl FromIterator<Name> for NameSet

Source§

fn from_iter<I: IntoIterator<Item = Name>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for Name

Source§

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

Writes the component chain, exactly as the derived implementation did when Name wrapped the component kind directly.

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 Name

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 Name

Source§

fn eq(&self, other: &Name) -> 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 Name

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

Auto Trait Implementations§

§

impl !Send for Name

§

impl !Sync for Name

§

impl Forget for Name

§

impl Freeze for Name

§

impl Move for Name

§

impl RefUnwindSafe for Name

§

impl Unpin for Name

§

impl UnsafeUnpin for Name

§

impl UnwindSafe for Name

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> 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> 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.