Struct GenericParamConstraintBuilder
pub struct GenericParamConstraintBuilder { /* private fields */ }Expand description
Builder for creating GenericParamConstraint metadata entries.
GenericParamConstraintBuilder provides a fluent API for creating GenericParamConstraint table entries
with validation and automatic table management. Generic parameter constraints define type restrictions
on generic parameters, enabling sophisticated type-safe programming with inheritance constraints,
interface requirements, value/reference type restrictions, and constructor constraints.
§Generic Constraint Model
.NET generic parameter constraints follow a structured pattern:
- Owner Parameter: The generic parameter that has this constraint applied
- Constraint Type: The type that the parameter must satisfy (base class, interface, etc.)
- Multiple Constraints: A parameter can have multiple constraint entries
- Constraint Hierarchy: Constraints interact with variance and inheritance rules
§Coded Index Types
Generic parameter constraints use specific table references:
- Owner: Direct GenericParam table index (RID or Token)
- Constraint:
TypeDefOrRefcoded index for the constraint type
§Constraint Types and Scenarios
Generic parameter constraints support various type restriction scenarios:
- Base Class Constraints:
where T : BaseClass(TypeDef/TypeRef) - Interface Constraints:
where T : IInterface(TypeDef/TypeRef) - Generic Type Constraints:
where T : IComparable<T>(TypeSpec) - Value Type Constraints:
where T : struct(handled via GenericParamAttributes) - Reference Type Constraints:
where T : class(handled via GenericParamAttributes) - Constructor Constraints:
where T : new()(handled via GenericParamAttributes)
§Multiple Constraints
A single generic parameter can have multiple constraint entries:
where T : BaseClass, IInterface1, IInterface2, new()This creates multiple GenericParamConstraint entries (one for BaseClass, one for each interface), plus GenericParamAttributes flags for the constructor constraint.
§Examples
let assembly = CilAssembly::new(view);
let mut context = BuilderContext::new(assembly);
// Create a base class constraint: where T : BaseClass
let generic_param_token = Token::new(0x2A000001); // GenericParam RID 1
let base_class_ref = CodedIndex::new(TableId::TypeDef, 1, CodedIndexType::TypeDefOrRef); // Local base class
let base_constraint = GenericParamConstraintBuilder::new()
.owner(generic_param_token)
.constraint(base_class_ref)
.build(&mut context)?;
// Create an interface constraint: where T : IComparable
let interface_ref = CodedIndex::new(TableId::TypeRef, 1, CodedIndexType::TypeDefOrRef); // External interface
let interface_constraint = GenericParamConstraintBuilder::new()
.owner(generic_param_token) // Same parameter can have multiple constraints
.constraint(interface_ref)
.build(&mut context)?;
// Create a generic interface constraint: where T : IEnumerable<string>
let generic_interface_spec = CodedIndex::new(TableId::TypeSpec, 1, CodedIndexType::TypeDefOrRef); // Generic type spec
let generic_constraint = GenericParamConstraintBuilder::new()
.owner(generic_param_token)
.constraint(generic_interface_spec)
.build(&mut context)?;
// Create constraints for a method-level generic parameter
let method_param_token = Token::new(0x2A000002); // GenericParam RID 2 (method parameter)
let system_object_ref = CodedIndex::new(TableId::TypeRef, 2, CodedIndexType::TypeDefOrRef); // System.Object
let method_constraint = GenericParamConstraintBuilder::new()
.owner(method_param_token)
.constraint(system_object_ref)
.build(&mut context)?;Implementations§
§impl GenericParamConstraintBuilder
impl GenericParamConstraintBuilder
pub fn new() -> Self
pub fn new() -> Self
Creates a new GenericParamConstraintBuilder.
§Returns
A new crate::metadata::tables::genericparamconstraint::GenericParamConstraintBuilder instance ready for configuration.
pub fn owner(self, owner: Token) -> Self
pub fn owner(self, owner: Token) -> Self
Sets the owning generic parameter.
The owner must be a valid GenericParam token that references a generic parameter defined in the current assembly. This establishes which generic parameter will have this constraint applied to it during type checking and instantiation.
Multiple constraints can be applied to the same parameter by creating multiple GenericParamConstraint entries with the same owner token.
Parameter types that can own constraints:
- Type-level parameters: Generic parameters defined on classes, interfaces, structs
- Method-level parameters: Generic parameters defined on individual methods
- Delegate parameters: Generic parameters defined on delegate types
§Arguments
owner- A GenericParam token pointing to the owning generic parameter
§Returns
Self for method chaining.
pub fn constraint(self, constraint: CodedIndex) -> Self
pub fn constraint(self, constraint: CodedIndex) -> Self
Sets the constraint type specification.
The constraint must be a valid TypeDefOrRef coded index that references
a type that the generic parameter must satisfy. This type becomes a compile-time
and runtime constraint that limits which types can be used as arguments for
the generic parameter.
Valid constraint types include:
TypeDef- Base classes and interfaces defined in the current assemblyTypeRef- External base classes and interfaces from other assembliesTypeSpec- Complex types including generic instantiations and constructed types
Common constraint scenarios:
- Base Class: Requires parameter to inherit from a specific class
- Interface: Requires parameter to implement a specific interface
- Generic Interface: Requires parameter to implement a generic interface with specific type arguments
- Constructed Type: Complex type relationships involving arrays, pointers, or nested generics
§Arguments
constraint- ATypeDefOrRefcoded index pointing to the constraint type
§Returns
Self for method chaining.
pub fn build(self, context: &mut BuilderContext) -> Result<Token>
pub fn build(self, context: &mut BuilderContext) -> Result<Token>
Builds the generic parameter constraint and adds it to the assembly.
This method validates all required fields are set, verifies the coded index types are correct, creates the raw constraint structure, and adds it to the GenericParamConstraint table with proper token generation and validation.
§Arguments
context- The builder context for managing the assembly
§Returns
A crate::metadata::token::Token representing the newly created generic parameter constraint, or an error if
validation fails or required fields are missing.
§Errors
- Returns error if owner is not set
- Returns error if constraint is not set
- Returns error if owner is not a valid GenericParam token
- Returns error if constraint is not a valid TypeDefOrRef coded index
- Returns error if table operations fail
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GenericParamConstraintBuilder
impl RefUnwindSafe for GenericParamConstraintBuilder
impl Send for GenericParamConstraintBuilder
impl Sync for GenericParamConstraintBuilder
impl Unpin for GenericParamConstraintBuilder
impl UnwindSafe for GenericParamConstraintBuilder
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