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: TypeDefOrRef coded 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

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

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

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 assembly
  • TypeRef - External base classes and interfaces from other assemblies
  • TypeSpec - 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 - A TypeDefOrRef coded index pointing to the constraint type
§Returns

Self for method chaining.

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§

§

impl Default for GenericParamConstraintBuilder

§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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, 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.