pub struct Type(/* private fields */);Expand description
A type in the Gruel type system.
After Phase 4.1 of ADR-0024, Type is a newtype wrapping a u32 index.
This enables O(1) type equality via u32 comparison.
§Encoding
The u32 value uses a tag-based encoding:
- Primitives (0-18): I8=0, I16=1, I32=2, I64=3, U8=4, U16=5, U32=6, U64=7, Isize=8, Usize=9, F16=10, F32=11, F64=12, Bool=13, Unit=14, Error=15, Never=16, ComptimeType=17, ComptimeStr=18, ComptimeInt=19
- Composites: low byte is tag (TAG_STRUCT, TAG_ENUM, TAG_ARRAY, TAG_MODULE), high 24 bits are the ID
§Usage
Use the associated constants for primitive types:
let ty = Type::I32;Use constructor methods for composite types:
let ty = Type::new_struct(struct_id);Use kind() for pattern matching:
match ty.kind() {
TypeKind::I32 => { /* ... */ }
TypeKind::Struct(id) => { /* ... */ }
_ => { /* ... */ }
}Implementations§
Source§impl Type
impl Type
Sourcepub const COMPTIME_TYPE: Type
pub const COMPTIME_TYPE: Type
The comptime type - the type of types themselves
Sourcepub const COMPTIME_STR: Type
pub const COMPTIME_STR: Type
The comptime string type - compile-time only string values
Sourcepub const COMPTIME_INT: Type
pub const COMPTIME_INT: Type
The comptime integer type - compile-time only integer values
Source§impl Type
impl Type
Sourcepub const fn new_struct(id: StructId) -> Type
pub const fn new_struct(id: StructId) -> Type
Create a struct type from a StructId.
Sourcepub const fn new_array(id: ArrayTypeId) -> Type
pub const fn new_array(id: ArrayTypeId) -> Type
Create an array type from an ArrayTypeId.
Sourcepub const fn new_ptr_const(id: PtrConstTypeId) -> Type
pub const fn new_ptr_const(id: PtrConstTypeId) -> Type
Create a raw const pointer type from a PtrConstTypeId.
Sourcepub const fn new_ptr_mut(id: PtrMutTypeId) -> Type
pub const fn new_ptr_mut(id: PtrMutTypeId) -> Type
Create a raw mut pointer type from a PtrMutTypeId.
Sourcepub const fn new_module(id: ModuleId) -> Type
pub const fn new_module(id: ModuleId) -> Type
Create a module type from a ModuleId.
Source§impl Type
impl Type
Sourcepub fn kind(&self) -> TypeKind
pub fn kind(&self) -> TypeKind
Get the kind of this type for pattern matching.
This method decodes the u32 representation back to a TypeKind for pattern matching.
Primitive types (0-12) decode directly; composite types decode the tag and ID.
§Panics
Panics if the Type has an invalid encoding. This should never happen with Types
created through the normal API. If you’re working with potentially corrupt data,
use try_kind instead.
§Example
match ty.kind() {
TypeKind::I32 | TypeKind::I64 => { /* handle integers */ }
TypeKind::Struct(id) => { /* handle struct */ }
_ => { /* other types */ }
}Sourcepub fn try_kind(&self) -> Option<TypeKind>
pub fn try_kind(&self) -> Option<TypeKind>
Try to get the kind of this type, returning None if the encoding is invalid.
This is the non-panicking version of kind. Use this when working
with potentially corrupt data or for defensive programming.
§Example
if let Some(kind) = ty.try_kind() {
match kind {
TypeKind::I32 => { /* ... */ }
_ => { /* ... */ }
}
} else {
eprintln!("corrupt type data");
}Sourcepub fn name(&self) -> &'static str
pub fn name(&self) -> &'static str
Get a human-readable name for this type.
Note: For struct and array types, this returns a placeholder.
Use type_name_with_structs for proper struct/array names.
Sourcepub fn safe_name_with_pool(&self, pool: Option<&TypeInternPool>) -> String
pub fn safe_name_with_pool(&self, pool: Option<&TypeInternPool>) -> String
Get a human-readable type name, safely handling anonymous structs and missing definitions.
Unlike name(), this method can access the type pool to get actual struct/enum names
instead of returning generic placeholders like "<struct>".
This is primarily used for error messages where we want to show meaningful type names even if the type pool lookup fails (returns safe fallback in that case).
§Safety
This method is safe even if the struct/enum ID is invalid or the pool is None.
It will return a fallback string like "<struct#123>" in those cases.
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Check if this type is an integer type. Optimized: checks tag range directly (0-9 are integer types: i8..u64, isize/usize).
Sourcepub fn is_comptime_type(&self) -> bool
pub fn is_comptime_type(&self) -> bool
Check if this is the comptime type (the type of types).
Sourcepub fn is_comptime_str(&self) -> bool
pub fn is_comptime_str(&self) -> bool
Check if this is the comptime string type.
Sourcepub fn is_comptime_int(&self) -> bool
pub fn is_comptime_int(&self) -> bool
Check if this is the comptime integer type.
Sourcepub fn as_array(&self) -> Option<ArrayTypeId>
pub fn as_array(&self) -> Option<ArrayTypeId>
Get the array type ID if this is an array type.
Sourcepub fn is_ptr_const(&self) -> bool
pub fn is_ptr_const(&self) -> bool
Check if this is a raw const pointer type.
Sourcepub fn as_ptr_const(&self) -> Option<PtrConstTypeId>
pub fn as_ptr_const(&self) -> Option<PtrConstTypeId>
Get the pointer type ID if this is a ptr const type.
Sourcepub fn is_ptr_mut(&self) -> bool
pub fn is_ptr_mut(&self) -> bool
Check if this is a raw mut pointer type.
Sourcepub fn as_ptr_mut(&self) -> Option<PtrMutTypeId>
pub fn as_ptr_mut(&self) -> Option<PtrMutTypeId>
Get the pointer type ID if this is a ptr mut type.
Sourcepub fn is_signed(&self) -> bool
pub fn is_signed(&self) -> bool
Check if this is a signed integer type. Signed integers: I8=0, I16=1, I32=2, I64=3, Isize=8.
Sourcepub fn is_float(&self) -> bool
pub fn is_float(&self) -> bool
Check if this is a floating-point type. Float types: F16=10, F32=11, F64=12.
Sourcepub fn is_numeric(&self) -> bool
pub fn is_numeric(&self) -> bool
Check if this is a numeric type (integer or float).
Sourcepub fn is_copy(&self) -> bool
pub fn is_copy(&self) -> bool
Check if this is a Copy type (can be implicitly duplicated).
Copy types are:
- All integer types (i8-i64, u8-u64)
- Boolean
- Unit
- Enum types
- Never type and Error type (for convenience in error recovery)
Non-Copy types (move types) are:
- Struct types (unless marked @copy, checked via StructDef.is_copy)
- Array types (unless element type is Copy, checked via Sema.is_type_copy)
Note: This method can’t check struct’s is_copy attribute or array element types since it doesn’t have access to StructDefs or array type information. Use Sema.is_type_copy() for full checking.
Sourcepub fn is_copy_in_pool(&self, type_pool: &TypeInternPool) -> bool
pub fn is_copy_in_pool(&self, type_pool: &TypeInternPool) -> bool
Check if this type is Copy, with access to TypeInternPool for struct checking.
This is used during anonymous struct creation to determine if the new struct should be Copy based on its field types.
Sourcepub fn is_64_bit(&self) -> bool
pub fn is_64_bit(&self) -> bool
Check if this is a 64-bit type (uses 64-bit operations). Optimized: checks for I64 (3) or U64 (7).
Sourcepub fn is_pointer_sized(&self) -> bool
pub fn is_pointer_sized(&self) -> bool
Check if this is a pointer-sized type (isize or usize). Checks for Isize (8) or Usize (9).
Sourcepub fn can_coerce_to(&self, target: &Type) -> bool
pub fn can_coerce_to(&self, target: &Type) -> bool
Check if this type can coerce to the target type.
Coercion rules:
- Never can coerce to any type (it represents divergent control flow)
- Error can coerce to any type (for error recovery during type checking)
- Otherwise, types must be equal
Sourcepub fn is_unsigned(&self) -> bool
pub fn is_unsigned(&self) -> bool
Check if this is an unsigned integer type. Unsigned integers: U8=4, U16=5, U32=6, U64=7, Usize=9.
Sourcepub fn literal_fits(&self, value: u64) -> bool
pub fn literal_fits(&self, value: u64) -> bool
Check if a u64 value fits within the range of this integer type.
For signed types, only the positive range is checked (0 to max positive).
Negation is handled separately to allow values like -128 for i8.
Returns true if the value fits, false otherwise.
For non-integer types, returns false.
Sourcepub fn negated_literal_fits(&self, value: u64) -> bool
pub fn negated_literal_fits(&self, value: u64) -> bool
Check if a u64 value can be negated to fit within the range of this signed integer type.
This is used to allow literals like 2147483648 when negated to -2147483648 (i32::MIN).
Returns true if the negated value fits, false otherwise.
Sourcepub fn as_u32(&self) -> u32
pub fn as_u32(&self) -> u32
Encode this type as a u32 for storage in extra arrays.
Since Type is now a u32 newtype, this simply returns the inner value.
Sourcepub fn from_u32(v: u32) -> Self
pub fn from_u32(v: u32) -> Self
Decode a type from a u32 value.
Since Type is now a u32 newtype, this simply wraps the value.
Note: This does not validate the encoding - use with values from as_u32().
§Safety (not unsafe, but correctness)
This method trusts that the input is a valid encoding. For untrusted data,
use try_from_u32 which validates the encoding.
Sourcepub fn try_from_u32(v: u32) -> Option<Self>
pub fn try_from_u32(v: u32) -> Option<Self>
Try to decode a type from a u32 value, returning None if invalid.
This validates that the encoding represents a valid type before returning. Use this when reading potentially corrupt data (e.g., deserialization, memory-mapped files, or debugging).
§Example
if let Some(ty) = Type::try_from_u32(encoded) {
// Safe to use ty.kind()
} else {
// Handle invalid encoding
}Sourcepub fn is_valid_encoding(v: u32) -> bool
pub fn is_valid_encoding(v: u32) -> bool
Check if a u32 value is a valid Type encoding.
Returns true if the value represents a valid primitive or composite type.
Trait Implementations§
impl Copy for Type
impl Eq for Type
impl StructuralPartialEq for Type
Auto Trait Implementations§
impl Freeze for Type
impl RefUnwindSafe for Type
impl Send for Type
impl Sync for Type
impl Unpin for Type
impl UnwindSafe for Type
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
§impl<'p, T> Seq<'p, T> for Twhere
T: Clone,
impl<'p, T> Seq<'p, T> for Twhere
T: Clone,
§type Iter<'a> = Once<&'a T>
where
T: 'a
type Iter<'a> = Once<&'a T> where T: 'a
§fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
§fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>where
'p: 'b,
fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>where
'p: 'b,
MaybeRef].