Skip to content

Commit

Permalink
correct canbeuninitialized (#517)
Browse files Browse the repository at this point in the history
* correct canbeuninitialized

* add test

* non-transitive custom serialization

---------

Co-authored-by: Jonas Isensee <jonas.isensee@ds.mpg.de>
  • Loading branch information
JonasIsensee and Jonas Isensee authored Dec 19, 2023
1 parent 7e934f3 commit 9365d0d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/data/custom_serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ h5convert_uninitialized!(out::Pointers, odr::Type{CustomSerialization{T,ODR}}) w
h5convert_uninitialized!(out, ODR)

jlconvert_canbeuninitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}) where {T,S,ODR} =
jlconvert_canbeuninitialized(ODR)
jlconvert_canbeuninitialized(ReadRepresentation{S,ODR}())
jlconvert_isinitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}, ptr::Ptr) where {T,S,ODR} =
jlconvert_isinitialized(ReadRepresentation{S,ODR}(), ptr)

Expand Down
22 changes: 19 additions & 3 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,16 @@ Base.@kwdef mutable struct T3 <: AT
t2::T2
end

DSA=Dict{Symbol, Any}
# Custom Serialization is not transitive, so using a custom serialization via a type
# that is custom serialized itself will not work
mutable struct PseudoDict{K,V}
kv::Vector{Pair{K,V}}
end

DSA=PseudoDict{Symbol, Any}
JLD2.writeas(::Type{T}) where {T <: AT} = DSA
JLD2.wconvert(::Type{DSA}, t::AT) = DSA(f => getproperty(t, f) for f in fieldnames(typeof(t)))
JLD2.rconvert(::Type{T}, dsa::DSA) where {T <: AT} = T(; dsa...)
JLD2.wconvert(::Type{DSA}, t::AT) = DSA([f => getproperty(t, f) for f in fieldnames(typeof(t))])
JLD2.rconvert(::Type{T}, dsa::DSA) where {T <: AT} = T(; dsa.kv...)

@testset "Issue #431 Identity-Preservation" begin
cd(mktempdir()) do
Expand Down Expand Up @@ -687,3 +693,13 @@ end
@test T == load("test.jld2", "T")
end
end


@testset "Issue #510 uninitialized Dicts" begin
cd(mktempdir()) do
save_object("test.jld2", Vector{Dict{String,Float64}}(undef, 10))
o = load_object("test.jld2")
@test !any(isassigned.(Ref(o), eachindex(o)))
end

end

0 comments on commit 9365d0d

Please sign in to comment.