Skip to main content
Version: dev

Type

std::meta::typ contains methods on the built-in Type type used for representing a type in the source program.

Methods

as_array

as_array
fn as_array(self) -> Option<(Type, Type)> {}

Source code: noir_stdlib/src/meta/typ.nr#L6-L8

If this type is an array, return a pair of (element type, size type).

Example:

comptime {
let array_type = quote { [Field; 3] }.as_type();
let (field_type, three_type) = array_type.as_array().unwrap();

assert(field_type.is_field());
assert_eq(three_type.as_constant().unwrap(), 3);
}

as_constant

as_constant
fn as_constant(self) -> Option<u32> {}

Source code: noir_stdlib/src/meta/typ.nr#L11-L13

If this type is a constant integer (such as the 3 in the array type [Field; 3]), return the numeric constant.

as_integer

as_integer
fn as_integer(self) -> Option<(bool, u8)> {}

Source code: noir_stdlib/src/meta/typ.nr#L16-L18

If this is an integer type, return a boolean which is true if the type is signed, as well as the number of bits of this integer type.

as_slice

as_slice
fn as_slice(self) -> Option<Type> {}

Source code: noir_stdlib/src/meta/typ.nr#L21-L23

If this is a slice type, return the element type of the slice.

as_str

as_str
fn as_str(self) -> Option<Type> {}

Source code: noir_stdlib/src/meta/typ.nr#L26-L28

If this is a str<N> type, returns the length N as a type.

as_struct

as_struct
fn as_struct(self) -> Option<(StructDefinition, [Type])> {}

Source code: noir_stdlib/src/meta/typ.nr#L31-L33

If this is a struct type, returns the struct in addition to any generic arguments on this type.

as_tuple

as_tuple
fn as_tuple(self) -> Option<[Type]> {}

Source code: noir_stdlib/src/meta/typ.nr#L36-L38

If this is a tuple type, returns each element type of the tuple.

get_trait_impl

get_trait_impl
fn get_trait_impl(self, constraint: TraitConstraint) -> Option<TraitImpl> {}

Source code: noir_stdlib/src/meta/typ.nr#L41-L43

Retrieves the trait implementation that implements the given trait constraint for this type. If the trait constraint is not found, None is returned. Note that since the concrete trait implementation for a trait constraint specified from a where clause is unknown, this function will return None in these cases. If you only want to know whether a type implements a trait, use implements instead.

Example:

comptime {
let field_type = quote { Field }.as_type();
let default = quote { Default }.as_trait_constraint();

let the_impl: TraitImpl = field_type.get_trait_impl(default).unwrap();
assert(the_impl.methods().len(), 1);
}

implements

implements
fn implements(self, constraint: TraitConstraint) -> bool {}

Source code: noir_stdlib/src/meta/typ.nr#L46-L48

true if this type implements the given trait. Note that unlike get_trait_impl this will also return true for any where constraints in scope.

Example:

fn foo<T>() where T: Default {
comptime {
let field_type = quote { Field }.as_type();
let default = quote { Default }.as_trait_constraint();
assert(field_type.implements(default));

let t = quote { T }.as_type();
assert(t.implements(default));
}
}

is_bool

is_bool
fn is_bool(self) -> bool {}

Source code: noir_stdlib/src/meta/typ.nr#L51-L53

true if this type is bool.

is_field

is_field
fn is_field(self) -> bool {}

Source code: noir_stdlib/src/meta/typ.nr#L56-L58

true if this type is Field.

Trait Implementations

impl Eq for Type

Note that this is syntactic equality, this is not the same as whether two types will type check to be the same type. Unless type inference or generics are being used however, users should not typically have to worry about this distinction.