Skip to content

Commit

Permalink
only convert if assigned (#530)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Isensee <[email protected]>
  • Loading branch information
JonasIsensee and Jonas Isensee authored Jan 11, 2024
1 parent 4463848 commit 202aa2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/data/custom_serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ writeas(T::Type) = T
# respectively. These fall back to convert.
wconvert(T, x) = convert(T, x)
rconvert(T, x) = convert(T, x)
rconvert(::Type{Array{T,N}}, x::Array{T2,N}) where {T, T2, N} = T[rconvert(T, y) for y in x]
rconvert(::Type{Array{T,N}}, x::Array{T,N}) where {T,N} = x
function rconvert(::Type{Array{T,N}}, x::Array{T2,N}) where {T, T2, N}
res = Array{T,N}(undef, size(x)...)
for i in eachindex(x)
if isassigned(x, i)
res[i] = rconvert(T, x[i])
end
end
res
end

# Select an ODR, incorporating custom serialization only if the types do not
# match
Expand Down Expand Up @@ -46,7 +55,7 @@ jlconvert_isinitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}, ptr:

function jlconvert(::ReadRepresentation{T,CustomSerialization{S,ODR}},
f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T,S,ODR}

if ismutabletype(T) && !(T <: Core.SimpleVector)
# May encounter a self-referential struct that used custom serialization
# provide an uninitialized struct and later fill it with values
Expand All @@ -66,4 +75,4 @@ function jlconvert(::ReadRepresentation{T,CustomSerialization{S,ODR}},
track_weakref!(f, header_offset, v)
return v
end
end
end
8 changes: 4 additions & 4 deletions src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ See also typestring.
"""
function shorttypestring(::Type{UnknownType{T, P}}) where {T, P}
tn = IOBuffer()
print(tn, T isa Symbol ? split(string(T),'.')[end] : T.name)
print(tn, T isa Symbol ? split(string(T),'.')[end] : T)
params = P.parameters
if !isempty(params)
write(tn, '{')
Expand Down Expand Up @@ -566,14 +566,14 @@ function constructrr(f::JLDFile, unk::Type{UnknownType{T,P}}, dt::CompoundDataty
else
@warn("read type $(typestring(unk)) was parametrized, but type " *
"$(T) in workspace is not; reconstructing")

end
elseif T isa UnionAll
body = behead(T)
if length(body.parameters) != length(P.parameters)
@warn("read type $(typestring(T)) has a different number of parameters from type " *
@warn("read type $(T) has a different number of parameters from type " *
"$(T) in workspace; reconstructing")
reconstruct_compound(f, typestring(T), dt, field_datatypes)
reconstruct_compound(f, string(T), dt, field_datatypes)
else
params = [P.parameters...,]
for i = 1:length(params)
Expand Down

0 comments on commit 202aa2b

Please sign in to comment.