Skip to main content
Version: dev

TypeDefinition

std::meta::type_def contains methods on the built-in TypeDefinition type. This type corresponds to struct Name { field1: Type1, ... } and enum Name { Variant1(Fields1), ... } items in the source program.

Methods​

add_abi​

add_abi
pub comptime fn add_abi(self, abi_argument: CtString) {}

Source code: noir_stdlib/src/meta/type_def.nr#L188-L190

Adds an abi attribute to the data type with the specified argument.

as_type​

as_type
pub comptime fn as_type(self) -> Type {}

Source code: noir_stdlib/src/meta/type_def.nr#L210-L212

Returns this type definition as a type in the source program. If this definition has any generics, the generics are also included as-is.

as_type_with_generics​

as_type_with_generics
pub comptime fn as_type_with_generics(self, generics: [Type]) -> Option<Type> {}

Source code: noir_stdlib/src/meta/type_def.nr#L237-L239

Returns a type from this type definition using the given generic arguments. Returns Option::none() if an incorrect amount of generic arguments are given for this type.

generics​

generics
pub comptime fn generics(self) -> [TypeGeneric] {

Source code: noir_stdlib/src/meta/type_def.nr#L361-L363

Returns each generic on this type definition as a TypeGeneric. Use typ() to get the generic's type variable. For a numeric generic, numeric_type() contains its numeric type; for other generics, it returns None.

Example:

#[example]
struct Foo<T, U, let K: u32> {
bar: [T; K],
baz: Baz<U, U>,
}

comptime fn example(foo: TypeDefinition) {
assert_eq(foo.generics().len(), 3);

// Fails because `T` isn't in scope
// let t = quote { T }.as_type();
// assert_eq(foo.generics()[0].typ(), t);
assert(foo.generics()[0].numeric_type().is_none());

// Last generic is numeric, so we have the numeric type available to us
assert(foo.generics()[2].numeric_type().is_some());
}

fields​

fields
pub comptime fn fields(self, generic_args: [Type]) -> [StructField] {

Source code: noir_stdlib/src/meta/type_def.nr#L389-L391

Returns each field in this struct type as a StructField. Use name() and typ() to inspect its name and type. visibility() returns the field's ItemVisibility. Generic types used in each field type are automatically substituted with the provided generic arguments.

fields_as_written​

fields_as_written
pub comptime fn fields_as_written(self) -> [StructField] {

Source code: noir_stdlib/src/meta/type_def.nr#L419-L421

Returns each field in this struct type as a StructField. Use name() and typ() to inspect its name and type. visibility() returns the field's ItemVisibility. Each type is returned as written, with any generic arguments unchanged. Unless the field types are not needed, users should generally prefer to use TypeDefinition::fields over this function if possible.

has_named_attribute​

has_named_attribute
pub comptime fn has_named_attribute<let N: u32>(self, name: str<N>) -> bool {}

Source code: noir_stdlib/src/meta/type_def.nr#L262-L264

Returns true if this type has a custom attribute with the given name.

This matches both built-in attributes and user-written attributes (tags and applied comptime macros). Use has_builtin_attribute if you need to match only the built-in attribute of the given name.

named_attribute_args​

named_attribute_args
pub comptime fn named_attribute_args<let N: u32>(self, name: str<N>) -> [[Quoted]] {}

Source code: noir_stdlib/src/meta/type_def.nr#L290-L292

Returns the arguments of each occurrence of the attribute with the given name, as token streams. The outer vector has one entry per occurrence — empty when the attribute is absent, so this is a superset of has_named_attribute — and each inner vector holds that occurrence's argument expressions, which can be spliced into generated code with quote.

has_builtin_attribute​

has_builtin_attribute
pub comptime fn has_builtin_attribute<let N: u32>(self, name: str<N>) -> bool {}

Source code: noir_stdlib/src/meta/type_def.nr#L315-L317

Returns true if this type has a built-in attribute with the given name.

Unlike has_named_attribute, this ignores user-written tag attributes and applied comptime macros.

location​

location
pub comptime fn location(self) -> Location {}

Source code: noir_stdlib/src/meta/type_def.nr#L340-L342

Returns the source Location where this type is defined. This can be passed to std::meta::error or std::meta::warn to attach a diagnostic to the type.

module​

module
pub comptime fn module(self) -> Module {}

Source code: noir_stdlib/src/meta/type_def.nr#L447-L449

Returns the module where the type is defined.

name​

name
pub comptime fn name(self) -> Quoted {}

Source code: noir_stdlib/src/meta/type_def.nr#L467-L469

Returns the name of this type

Note that the returned quoted value will be just the type name, it will not be the full path to the type definition, nor will it include any generics.

Trait Implementations​

impl Eq for TypeDefinition
impl Hash for TypeDefinition

Note that each type definition is assigned a unique ID internally and this is what is used for equality and hashing. So even type definitions with identical generics and fields may not be equal in this sense if they were originally different items in the source program.