Skip to content

Commit

Permalink
reuse _unsafe_cast strategy from apache#103
Browse files Browse the repository at this point in the history
  • Loading branch information
jrevels committed Jan 9, 2021
1 parent c5c7aed commit b7bb22d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/arraytypes/fixedsizelist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,30 @@ Base.size(l::FixedSizeList) = (l.ℓ,)
else
off = (i - 1) * N
if X === T && isbitstype(Y)
tup = reinterpret(NTuple{N, Y}, view(l.data, (off + 1):(off + N)))[]
tup = _unsafe_load_tuple(NTuple{N,Y}, l.data, off)
else
tup = ntuple(j->l.data[off + j], N)
end
return ArrowTypes.arrowconvert(T, tup)
end
end

function _unsafe_load_tuple(::Type{NTuple{N,T}}, bytes::Vector{UInt8}, i::Integer) where {N,T}
a = Ref(bytes, i)
b = Ref{NTuple{N,T}}()
return _unsafe_cast(a, b, N)
end

function _unsafe_cast(a::Ref{T}, b::Ref{B}, n::Integer)::B where {T,B}
A = eltype(T)
GC.@preserve a b begin
ptra = Base.unsafe_convert(Ptr{A}, a)
ptrb = Base.unsafe_convert(Ptr{B}, b)
unsafe_copyto!(Ptr{A}(ptrb), ptra, n)
end
return b[]
end

@propagate_inbounds function Base.setindex!(l::FixedSizeList{T}, v::T, i::Integer) where {T}
@boundscheck checkbounds(l, i)
if v === missing
Expand Down

0 comments on commit b7bb22d

Please sign in to comment.