JuliaIO / BSON.jl

Other
158 stars 39 forks source link

Cannot save array of structs with different width member #92

Open david-andrew opened 3 years ago

david-andrew commented 3 years ago

An error occurs when trying to save an array containing structs where the bitwidth of the members is different.

Here's a minimal example:

julia> using BSON

julia> struct MyStruct
           a::Int32
           b::Float64
       end

julia> var = [MyStruct(1,1.0), MyStruct(2, 2.0)]
2-element Array{MyStruct,1}:
 MyStruct(1, 1.0)
 MyStruct(2, 2.0)

julia> BSON.@save "test.bson" var
ERROR: Padding of type UInt8 is not compatible with type MyStruct.
Stacktrace:
 [1] check_readable at ./reinterpretarray.jl:81 [inlined]
 [2] getindex at ./reinterpretarray.jl:125 [inlined]
 [3] iterate at ./abstractarray.jl:986 [inlined]
 [4] iterate at ./abstractarray.jl:984 [inlined]
 [5] reinterpret_(::Type{UInt8}, ::Array{MyStruct,1}) at /home/dsamson/.julia/packages/BSON/XAts7/src/extensions.jl:67
 [6] lower at /home/dsamson/.julia/packages/BSON/XAts7/src/extensions.jl:72 [inlined]
 [7] _lower_recursive(::Array{MyStruct,1}, ::IdDict{Any,Any}, ::Array{Any,1}) at /home/dsamson/.julia/packages/BSON/XAts7/src/write.jl:62
 [8] (::BSON.var"#5#9"{IdDict{Any,Any},Array{Any,1}})(::Array{MyStruct,1}) at /home/dsamson/.julia/packages/BSON/XAts7/src/write.jl:62
 [9] applychildren!(::BSON.var"#5#9"{IdDict{Any,Any},Array{Any,1}}, ::Dict{Symbol,Any}) at /home/dsamson/.julia/packages/BSON/XAts7/src/BSON.jl:21
 [10] _lower_recursive(::Dict{Symbol,Array{MyStruct,1}}, ::IdDict{Any,Any}, ::Array{Any,1}) at /home/dsamson/.julia/packages/BSON/XAts7/src/write.jl:62
 [11] lower_recursive(::Dict{Symbol,Array{MyStruct,1}}) at /home/dsamson/.julia/packages/BSON/XAts7/src/write.jl:73
 [12] bson(::IOStream, ::Dict{Symbol,Array{MyStruct,1}}) at /home/dsamson/.julia/packages/BSON/XAts7/src/write.jl:81
 [13] #12 at /home/dsamson/.julia/packages/BSON/XAts7/src/write.jl:83 [inlined]
 [14] open(::BSON.var"#12#13"{Dict{Symbol,Array{MyStruct,1}}}, ::String, ::Vararg{String,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at ./io.jl:325
 [15] open at ./io.jl:323 [inlined]
 [16] bson(::String, ::Dict{Symbol,Array{MyStruct,1}}) at /home/dsamson/.julia/packages/BSON/XAts7/src/write.jl:83
 [17] top-level scope at REPL[6]:1

I've tested it with the following structs:

struct MyStruct2  #works correctly
    a::Int32
end

struct MyStruct3  #fails as above
    a::Int32
    b::Int64
end

struct MyStruct4  #works correctly
    a::Int32
    b::Int32
end

I discovered this error while trying to save SVM objects generated by LIBSVM, which makes use of such different width structs for the sake of compatibility with an underlying C++ interface.