Skip to main content

DeclId

Struct DeclId 

Source
#[repr(transparent)]
pub struct DeclId(pub u128);
Expand description

Identifies a type declaration, independent of type parameters.

Think of DeclId as a “type-parameter-erased” version of Shape::id. While Vec<u32> and Vec<String> have different Shape::id values, they share the same DeclId because they come from the same Vec<T> declaration.

§How DeclId is computed

§Non-generic types

For types without type parameters (like u32 or MyStruct), DeclId is trivial—it can simply equal the hash of the type identifier. When implementing Facet for such types, you don’t need to do anything special; if you don’t call .decl_id() on the builder, it’s automatically computed from the type_identifier.

§Generic types with #[derive(Facet)]

For generic types using the derive macro, DeclId is computed from:

file!():line!():column!()#kind#TypeName

For example: src/lib.rs:42:1#struct#Wrapper

This strategy assumes no two declarations exist at the exact same source location. This isn’t strictly true with macros that generate multiple types, which is why we also include the type name. Even so, this can be defeated in edge cases—it’s a best-effort approach.

§Foreign generic types (manual Facet implementations)

When implementing Facet for an external generic type (like Vec, Arc, HashMap), set the module_path on the builder:

ShapeBuilder::for_sized::<Arc<T>>("Arc")
    .module_path("alloc::sync")
    // ... other fields ...
    .build()

The DeclId is then auto-computed from: @{module_path}#{kind}#{type_identifier}

For example, Arc in alloc::sync with type struct produces: @alloc::sync#struct#Arc

This is also not foolproof—if you have two different versions of the same crate in your dependency graph, they’ll produce the same DeclId even though they’re technically different declarations.

§Example

use facet::Facet;

#[derive(Facet)]
struct Wrapper<T> {
    inner: T,
}

// Different types (different Shape::id)
assert!(<Wrapper<u32>>::SHAPE.id != <Wrapper<String>>::SHAPE.id);

// Same declaration (same Shape::decl_id)
assert!(<Wrapper<u32>>::SHAPE.decl_id == <Wrapper<String>>::SHAPE.decl_id);

§Stability

DeclId is completely opaque and provides no stability guarantees:

  • NOT stable across different compilations
  • NOT stable across refactors (adding a comment changes line numbers)
  • NOT stable across reformatting (column numbers change)
  • NOT suitable for persistence or serialization

The only guarantee: within a single compilation, the same declaration produces the same DeclId.

This is sufficient for runtime use cases like “group all instantiations of Vec<T> in this program” but NOT for cross-compilation comparisons or persistence.

Tuple Fields§

§0: u128

Implementations§

Source§

impl DeclId

Source

pub const fn new(hash: u128) -> Self

Create a DeclId from a raw hash value.

This is typically called by the derive macro using const_fnv1a_hash.

Trait Implementations§

Source§

impl Clone for DeclId

Source§

fn clone(&self) -> DeclId

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for DeclId

Source§

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

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

impl Hash for DeclId

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 DeclId

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

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

impl PartialEq for DeclId

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for DeclId

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 Copy for DeclId

Source§

impl Eq for DeclId

Source§

impl StructuralPartialEq for DeclId

Auto Trait Implementations§

§

impl Freeze for DeclId

§

impl RefUnwindSafe for DeclId

§

impl Send for DeclId

§

impl Sync for DeclId

§

impl Unpin for DeclId

§

impl UnwindSafe for DeclId

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.