Skip to content

Refactor Mun ABI to safely support StructInfo in TypeInfo #276

@Wodann

Description

@Wodann

Currently, StructInfo is appended to a TypeInfo struct - for types that are structs. We can obtain the StructInfo as follows:

/// Retrieves the type's struct information, if available.
pub fn as_struct(&self) -> Option<&StructInfo> {
    if self.group.is_struct() {
        let ptr = (self as *const TypeInfo).cast::<u8>();
        let ptr = ptr.wrapping_add(mem::size_of::<TypeInfo>());
        let offset = ptr.align_offset(mem::align_of::<StructInfo>());
        let ptr = ptr.wrapping_add(offset);
        Some(unsafe { &*ptr.cast::<StructInfo>() })
    } else {
        None
    }
}

As this is unsafe, we want to add proper ABI support for StructInfo inside the TypeInfo, by adding:

#[derive(AsValue)]
#[repr(u8)]
pub enum TypeInfoData {
    Primitive,
    Struct(StructInfo),
}

to the TypeInfo:

#[derive(AsValue)]
pub struct TypeInfo {
    ...
    pub data: TypeInfoData,
}

Depends on #275

Metadata

Metadata

Assignees

Labels

exp: intermediateAchievable by experienced contributors, or with some guidancepri: intermediateAn issue resulting in non-critical functionality loss and no significant effect on usabilitytype: refactorRefactor existing code

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions