Flecs-Rust icon indicating copy to clipboard operation
Flecs-Rust copied to clipboard

Enhancing Support for Rust Enums with Variable Field Sizes in Flecs C Core

Open Indra-db opened this issue 1 year ago • 0 comments

The current implementation of the binding is restricted to handling Enum components where each field is mandated to be 4 bytes in size. This contrasts with Rust's default behavior, which allocates 1 byte per field. Moreover, Rust Enums are capable of accommodating fields of any size, including complex data structures akin to C++'s std::variant.

To extend our support to encompass fully-featured Rust Enums, modifications are necessitated within the C core of Flecs. Preliminary investigations suggest that these adjustments are feasible.

The existing approach to facilitate the compatibility of enum components entails annotating them with Repr(C), ensuring that each field occupies 4 bytes.

#[repr(C)]
#[derive(Debug, Default, Clone, Component)]
enum Color {
    Green,
    #[default]
    Red,
    Blue,
}

Indra-db avatar Mar 07 '24 04:03 Indra-db