Reinterpret Tuple as Struct, and vector thereof

This works, for example:

julia> a = rand(SVector{2,Int}, 10);

julia> b = reinterpret(IntPoint, a);

and no messing around with unsafe pointer operations.

The result is a Base.ReinterpretArray{IntPoint, 1}, which is a subtype of AbstractVector{IntPoint}. Unfortunately, the Clipper.jl library currently only supports Vector. One way around this would be to do:

julia> v = unsafe_wrap(Array, pointer(b), length(b))

to get a v of type Vector{IntPoint}. Be sure to wrap GC.@protect b begin ... end around any code that needs to use v, to ensure that the underlying array doesn’t get garbage-collected while you are using it.

4 Likes