Skip to content

Properly handle bit flags #15

@clickingbuttons

Description

@clickingbuttons

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,
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions