Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle bit flags #15

Open
clickingbuttons opened this issue Jun 22, 2023 · 1 comment
Open

Properly handle bit flags #15

clickingbuttons opened this issue Jun 22, 2023 · 1 comment

Comments

@clickingbuttons
Copy link

For the following FBS:

enum AdvancedFeatures : ulong (bit_flags) {
    AdvancedArrayFeatures,
    AdvancedUnionFeatures,
    OptionalScalars,
    DefaultVectorsAndStrings,
}

The following enum is generated:

pub const AdvancedFeatures = enum(u64) {
    AdvancedArrayFeatures = 1,
    AdvancedUnionFeatures = 2,
    OptionalScalars = 4,
    DefaultVectorsAndStrings = 8,
    pub fn tagName(v: @This()) []const u8 {
        return @tagName(v);
    }
};

Which causes the @intToEnum cast to fail if multiple bit flags (or none) are set:

    pub fn AdvancedFeatures(rcv: Schema) reflection.AdvancedFeatures {
        const o = rcv._tab.offset(16);
        if (o != 0) {
            return rcv._tab.read(reflection.AdvancedFeatures, o + rcv._tab.pos);
        }
        return @intToEnum(reflection.AdvancedFeatures, 0);
    }

One potential fix is to generate a packed struct instead of an enum and read it like you would any other scalar field. The packed struct looks like this:

pub const AdvancedFeatures = packed struct {
    AdvancedArrayFeatures: bool,
    AdvancedUnionFeatures: bool,
    OptionalScalars: bool,
    DefaultVectorsAndStrings: bool,
};
@Ev1lT3rm1nal
Copy link

It can be a packed struct but for keeping the code simple u1 can be used too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants