Struct Token

pub struct Token(pub u32);
Expand description

A metadata token representing a reference to a specific entry within a metadata table.

Tokens are the fundamental addressing mechanism in .NET metadata, providing a uniform way to reference entries across different metadata tables. Each token encodes both the target table type and the specific row within that table.

§Token Format

A token is a 32-bit value structured as follows:

┌─────────────┬───────────────────────────────────┐
│ Table (8b)  │        Row Index (24b)            │
├─────────────┼───────────────────────────────────┤
│   31-24     │            23-0                   │
└─────────────┴───────────────────────────────────┘

§Table Identification

The high byte identifies the metadata table type according to ECMA-335:

  • 0x01: TypeRef - References to external types
  • 0x02: TypeDef - Type definitions within this assembly
  • 0x04: FieldDef - Field definitions
  • 0x06: MethodDef - Method definitions
  • 0x0A: MemberRef - References to external members
  • And many others…

§Row Addressing

The low 24 bits provide a 1-based index into the specified table, allowing for up to 16,777,215 entries per table. A row index of 0 indicates a null reference.

§Usage in IL and Metadata

Tokens appear in multiple contexts:

  • IL Instructions: call, newobj, ldtoken, etc.
  • Table References: Cross-references between metadata tables
  • Debug Information: Linking debug data to metadata
  • Reflection API: Internal representation of metadata references

Tuple Fields§

§0: u32

Implementations§

§

impl Token

pub fn new(value: u32) -> Self

Creates a new token from a raw 32-bit value.

This constructor accepts any 32-bit value and interprets it as a metadata token according to the ECMA-335 token format. No validation is performed on the input value, allowing for the creation of tokens that may not correspond to valid metadata entries.

§Arguments
  • value - The raw 32-bit token value with table ID in high byte and row index in low 24 bits
§Returns

A new Token instance wrapping the provided value.

pub fn value(&self) -> u32

Returns the raw 32-bit token value.

This method provides access to the underlying token representation as used in IL instructions, metadata tables, and the .NET reflection API.

§Returns

The complete 32-bit token value including both table identifier and row index.

pub fn table(&self) -> u8

Extracts the table identifier from the token.

Returns the high byte (bits 24-31) of the token, which identifies the metadata table type according to ECMA-335 specifications. This value corresponds to the table enumeration used throughout the metadata system.

§Returns

The 8-bit table identifier (0x00-0xFF).

§Common Table IDs
  • 0x01: TypeRef
  • 0x02: TypeDef
  • 0x04: FieldDef
  • 0x06: MethodDef
  • 0x0A: MemberRef
  • 0x1B: TypeSpec
  • 0x2B: MethodSpec

pub fn row(&self) -> u32

Extracts the row index from the token.

Returns the low 24 bits (bits 0-23) of the token, which represent the 1-based row index within the specified metadata table. A value of 0 indicates a null reference that doesn’t point to any table entry.

§Returns

The 24-bit row index (0-16,777,215), where 0 represents null.

§Row Index Semantics
  • 1-based indexing: Valid row indexes start from 1
  • Null reference: Row index 0 indicates no target entry
  • Maximum capacity: Up to 16,777,215 entries per table

pub fn is_null(&self) -> bool

Returns true if this token represents a null reference.

A null token has a value of 0, indicating that it doesn’t reference any metadata table entry. Null tokens are used in metadata to represent optional or absent references.

§Returns

true if the token value is 0, false otherwise.

§Usage in Metadata

Null tokens commonly appear in:

  • Optional parent type references
  • Unused coded index entries
  • Default value placeholders
  • Conditional reference fields

Trait Implementations§

§

impl Clone for Token

§

fn clone(&self) -> Token

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
§

impl Debug for Token

§

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

Formats the token for debugging output with detailed breakdown.

The debug format includes the raw token value in hexadecimal, the table identifier, and the row index for comprehensive debugging information.

§Format

Token(0x{:08x}, table: 0x{:02x}, row: {})

§

impl Display for Token

§

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

Formats the token for user display as a hexadecimal value.

The display format shows only the raw token value in standard hexadecimal notation, suitable for user-facing output and logging.

§Format

0x{:08x} - 8-digit uppercase hexadecimal with 0x prefix

§

impl From<Token> for u32

§

fn from(token: Token) -> Self

Extracts the raw 32-bit value from a Token.

This conversion provides access to the underlying token representation for use in contexts requiring raw token values, such as IL generation or metadata serialization.

§

impl From<u32> for Token

§

fn from(value: u32) -> Self

Converts a raw 32-bit value into a Token.

This conversion allows for ergonomic token creation from integer literals and variables. The conversion is identical to Token::new but provides the standard Rust conversion trait interface.

§

impl Hash for Token

§

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

Computes the hash value for this token.

The hash is computed directly from the underlying 32-bit value, providing efficient and consistent hashing for use in hash-based collections like HashMap and HashSet.

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
§

impl Ord for Token

§

fn cmp(&self, other: &Token) -> 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
§

impl PartialEq for Token

§

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

impl PartialOrd for Token

§

fn partial_cmp(&self, other: &Token) -> 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
§

impl Copy for Token

§

impl Eq for Token

§

impl StructuralPartialEq for Token

Auto Trait Implementations§

§

impl Freeze for Token

§

impl RefUnwindSafe for Token

§

impl Send for Token

§

impl Sync for Token

§

impl Unpin for Token

§

impl UnwindSafe for Token

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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> Same for T

Source§

type Output = T

Should always be Self
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 = 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.