Skip to content

Commit 1ef7bb5

Browse files
committed
fix size of the wrapped array and make sure the array is preserved during copying
1 parent 083c06b commit 1ef7bb5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/extensions.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,25 @@ tags[:unionall] = d -> UnionAll(d[:var], d[:body])
9090
lower(x::Vector{Any}) = copy(x)
9191
lower(x::Vector{UInt8}) = x
9292

93+
const WARN_PADDING = Ref(true)
94+
9395
function reinterpret_(::Type{T}, x) where T
9496
r = reinterpret(T, x)
9597
if r isa Base.ReinterpretArray && !(r.readable)
9698
# type mapping was successful, but the array is not readable due to padding
97-
# in that case make r a type-cast view of the original array
99+
# in that case make use unsafe_wrap() to map the data
98100
# the data might not be restorable on a different machine with different padding or with a different version of Julia
99-
@warn "storing structure with padding, data might not be restorable"
100-
r = unsafe_wrap(Vector{T}, Ptr{T}(pointer(x)), sizeof(x))
101+
WARN_PADDING[] && @warn """
102+
Storing structure with padding, data might not be restorable.\n
103+
To suppress this warning, set BSON.WARN_PADDING[] = false.
104+
"""
105+
GC.@preserve x begin
106+
a = unsafe_wrap(Vector{T}, Ptr{T}(pointer(x)), sizeof(x) ÷ sizeof(T))
107+
T[_x for _x in a]
108+
end
109+
else
110+
T[_x for _x in r]
101111
end
102-
T[_x for _x in r]
103112
end
104113

105114
function lower(x::Array)

0 commit comments

Comments
 (0)