Type
std::meta::typ
contains methods on the built-in Type
type used for representing
a type in the source program.
Methods
as_array
fn as_array(self) -> Option<(Type, Type)> {}
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
fn as_constant(self) -> Option<u32> {}
If this type is a constant integer (such as the 3
in the array type [Field; 3]
),
return the numeric constant.
as_integer
fn as_integer(self) -> Option<(bool, u8)> {}
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
fn as_slice(self) -> Option<Type> {}
If this is a slice type, return the element type of the slice.
as_str
fn as_str(self) -> Option<Type> {}
If this is a str<N>
type, returns the length N
as a type.
as_struct
fn as_struct(self) -> Option<(StructDefinition, [Type])> {}
If this is a struct type, returns the struct in addition to any generic arguments on this type.
as_tuple
fn as_tuple(self) -> Option<[Type]> {}
If this is a tuple type, returns each element type of the tuple.
get_trait_impl
fn get_trait_impl(self, constraint: TraitConstraint) -> Option<TraitImpl> {}
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
fn implements(self, constraint: TraitConstraint) -> bool {}
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
fn is_bool(self) -> bool {}
true
if this type is bool
.
is_field
fn is_field(self) -> bool {}
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.