Skip to content

Rust default for array #8436

Open
Open
@DolajoCZ

Description

@DolajoCZ

Issue

Invalid generation of rust code with switch --gen-object-api and struct containing array of size 33 and more.

Reason

Generated object api struct implement Default trait by derive macro, but array implement Default trait only for size up to 32 (source).

Example of invalid fbs

Fbs file example.fbs

namespace example;

struct FooStruct {
	array_values: [double:81];
}

table FooTable {
	foo_struct: FooStruct;
}

root_type FooTable;

Command

flatc.exe --rust --gen-object-api example.fbs

Invalid code section

    #[derive(Debug, Clone, PartialEq, Default)]
    pub struct FooStructT {
        pub array_values: [f64; 81],
    }

Tool versions

flatc - 24.3.25
rustc - 1.82.0

Potential solution

For struct with array implement custom Default trait.

Example of solution from code above

    #[derive(Debug, Clone, PartialEq)]
    pub struct FooStructT {
        pub array_values: [f64; 81],
    }

    impl std::default::Default for FooStructT {
        fn default() -> Self {
            Self {
                array_values: [Default::default(); 81],
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions