Skip to content

Commit

Permalink
fix: do not assume AbstractVectorOfArray is mutable in Base.zero
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Oct 30, 2024
1 parent 600a9b5 commit 08e2d13
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/vector_of_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ end

function Base.zero(VA::AbstractVectorOfArray)
val = copy(VA)
val.u = zero.(VA.u)
val.u .= zero.(VA.u)
return val
end

Expand Down
10 changes: 10 additions & 0 deletions test/interface_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,13 @@ end
f3!(z, zz)
@test z == VectorOfArray([fill(4, SVector{2, Float64}), fill(2, SVector{2, Float64})])
@test (@allocated f3!(z, zz)) == 0

struct ImmutableVectorOfArray{T, N, A} <: AbstractVectorOfArray{T, N, A}
u::A # A <: AbstractArray{<: AbstractArray{T, N - 1}}
end

@testset "Base.zero does not assume mutable struct" begin
voa = ImmutableVectorOfArray{Float64, 2, Vector{Vector{Float64}}}([ones(3), 2ones(3)])
zvoa = zero(voa)
@test voa.u[1] == voa.u[2] == zeros(3)
end

0 comments on commit 08e2d13

Please sign in to comment.