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: u32Implementations§
§impl Token
impl Token
pub fn new(value: u32) -> Self
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
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
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
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
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 Debug for Token
impl Debug for Token
§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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 Ord for Token
impl Ord for Token
§impl PartialOrd for Token
impl PartialOrd for Token
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> 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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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