Struct GenericParam
pub struct GenericParam {
pub rid: u32,
pub token: Token,
pub offset: usize,
pub number: u32,
pub flags: u32,
pub owner: OnceLock<CilTypeReference>,
pub constraints: CilTypeRefList,
pub name: String,
pub custom_attributes: CustomAttributeValueList,
}Expand description
Represents a generic parameter definition with resolved references and owned data.
A generic parameter defines a type or method parameter that can be substituted with concrete types during instantiation. This enables generic programming with type safety and performance benefits.
§Generic Parameter Types
Parameters can be defined at different scopes:
- Type parameters: Defined on classes, interfaces, and delegates
- Method parameters: Defined on individual methods
- Nested parameters: Parameters within generic types can have their own parameters
§Parameter Characteristics
Each parameter has several important properties:
- Position: Ordinal position in the parameter list (0-based)
- Name: Identifier used in signatures and source code
- Constraints: Restrictions on acceptable type arguments
- Variance: Covariance/contravariance for assignment compatibility
- Owner: The type or method that declares the parameter
§Variance Support
Generic parameters can specify variance for type safety:
┌──────────────┬─────────────────┬─────────────────────────────┐
│ Variance │ Keyword │ Assignment Compatibility │
├──────────────┼─────────────────┼─────────────────────────────┤
│ Invariant │ (none) │ Exact type match required │
│ Covariant │ out │ Derived → Base allowed │
│ Contravariant│ in │ Base → Derived allowed │
└──────────────┴─────────────────┴─────────────────────────────┘§Constraint Types
Parameters can have various constraints:
- Class constraint:
where T : class(reference types only) - Struct constraint:
where T : struct(value types only) - Constructor constraint:
where T : new()(parameterless constructor) - Base class constraint:
where T : BaseClass(inheritance requirement) - Interface constraints:
where T : IInterface(implementation requirement)
§Owner Resolution
Generic parameters are owned by either types or methods:
- Type ownership: Parameters declared on generic types
- Method ownership: Parameters declared on generic methods
- Lazy resolution: Owner is resolved when first accessed
- Type reference: Uses
CilTypeReferencefor unified handling
§ECMA-335 Reference
See ECMA-335, Partition II, §22.20 for the complete GenericParam table specification.
Fields§
§rid: u32The row identifier in the GenericParam table.
This 1-based index uniquely identifies this generic parameter within the GenericParam table.
Combined with the table type, it forms the parameter’s unique identity.
token: TokenThe metadata token for this generic parameter.
A crate::metadata::token::Token that uniquely identifies this generic parameter across the entire assembly.
The token encodes both the table type (GenericParam) and the row ID.
offset: usizeThe byte offset of this generic parameter in the metadata tables stream.
This offset points to the start of this parameter’s row data within the metadata tables stream, used for binary parsing and navigation.
number: u32The ordinal position of this parameter in the parameter list.
A 2-byte index indicating the parameter’s position, numbered left-to-right starting from zero. This determines the parameter’s position in generic instantiations and signature matching.
flags: u32Generic parameter attribute flags indicating constraints and variance.
A 2-byte bitmask of GenericParamAttributes values that specify:
- Variance: Covariant, contravariant, or invariant
- Constraints: Reference type, value type, constructor constraints
- Special flags: Additional constraint information
owner: OnceLock<CilTypeReference>Reference to the owner of this generic parameter.
A lazily-initialized CilTypeReference that points to either:
TypeDef: For type-level generic parametersMethodDef: For method-level generic parameters
Uses OnceLock for thread-safe lazy initialization during owner resolution.
constraints: CilTypeRefListList of constraint types that apply to this parameter.
A collection of CilTypeReference entries that specify the constraints
imposed on this generic parameter, such as base classes and interfaces
that type arguments must satisfy.
name: StringThe name of the generic parameter.
The parameter name as it appears in source code and metadata, resolved from the strings heap. Used for reflection and debugging purposes.
custom_attributes: CustomAttributeValueListCustom attributes applied to this generic parameter.
A collection of custom attributes that provide additional metadata about the parameter, such as documentation or analysis annotations.
Implementations§
§impl GenericParam
impl GenericParam
pub fn apply(self: &Arc<Self>) -> Result<()>
pub fn apply(self: &Arc<Self>) -> Result<()>
Apply this generic parameter to its owner type or method.
This method associates the generic parameter with its owner by adding it to
the owner’s parameter collection. The owner can be either a generic type
(TypeDef) or a generic method (MethodDef).
§Owner Types
The owner can be one of two types:
TypeDef: Generic types with type parameters (List<T>)MethodDef: Generic methods with method parameters (Method<U>())
§Returns
Returns Ok(()) on successful application, or an error if:
- Owner reference is not set or invalid
- Owner type is not
TypeDeforMethodDef - Method reference is weak and has been dropped
- Parameter collection operations fail
§Errors
- No Owner: If the owner type reference is not set
- Invalid Owner: If the owner is not a supported type or method reference
- Weak Reference: If a method reference has been dropped
§Runtime Impact
After successful application, the parameter will:
- Be available in the owner’s parameter collection
- Participate in generic instantiation and signature matching
- Support constraint checking during type resolution
- Enable reflection and metadata queries
Auto Trait Implementations§
impl !Freeze for GenericParam
impl RefUnwindSafe for GenericParam
impl Send for GenericParam
impl Sync for GenericParam
impl Unpin for GenericParam
impl UnwindSafe for GenericParam
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> 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