Skip to content

Commit c4c54b9

Browse files
author
Pietro Vertechi
authored
fix copy for views (#170)
* fix copy for views * do not recurse * comment * avoid eachcol in tests
1 parent 8a43e48 commit c4c54b9

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/structarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ for op in [:cat, :hcat, :vcat]
398398
end
399399
end
400400

401-
Base.copy(s::StructArray{T,N,C}) where {T,N,C} = StructArray{T,N,C}(C(copy(x) for x in components(s)))
401+
Base.copy(s::StructArray{T}) where {T} = StructArray{T}(map(copy, components(s)))
402402

403403
function Base.reshape(s::StructArray{T}, d::Dims) where {T}
404404
StructArray{T}(map(x -> reshape(x, d), components(s)))

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ end
278278
t2[1,1] = 2.0 + im*4.0
279279
# Test we actually did a copy
280280
@test t[1,1] == 1.0 + im*4.0
281+
# Test that `copy` works, even when the array type changes (e.g. views)
282+
s = rand(10, 2)
283+
v = StructArray{ComplexF64}((view(s, :, 1), view(s, :, 2)))
284+
v2 = copy(v)
285+
@test v2.re isa Vector
286+
@test v2.im isa Vector
287+
@test v2.re == copy(v.re)
288+
@test v2.im == copy(v.im)
281289
end
282290

283291
@testset "undef initializer" begin

0 commit comments

Comments
 (0)