Skip to content

Add safe casting from one Storage Subset to another #274

@hubidubi13

Description

@hubidubi13

Sometimes I write helper functions that need access to specific components of the ECS storage. These functions might be used from different systems, with different storage subsets. It would be nice, if there was some sort of compile time checked casting between different subsets, maybe like this:

const Storage = ecez.CreateStorage(.{
    Position,
    Velocity,
});

const All = Storage.Subset(.{
    *Position,
    *Velocity,
});

const PosRead = Storage.Subset(.{Position});

var storage = Storage.init(alloc);
var all = All{ .storage = &storage };
var pos_read = PosRead{ .storage = &storage };

// casting
const all_cast = pos_read.cast(All); // -> Not allowed, compile error
const pos_read_cast = all.cast(PosRead); // -> Allowed, returns *PosRead

Currently, I am simply using @ptrCast. It works, but there are no checks. If this could be done automatically by coercion somehow, it would be magical! But I don't think that is currently possible with the zig type system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalA *potential* improvement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions