Skip to content

Commit 49f3181

Browse files
Merge pull request #276 from DrChainsaw/copychtype
Remove type constraints for copy of ArrayPartition
2 parents 6e8600f + dbc72a5 commit 49f3181

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/array_partition.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ function Base.similar(A::ArrayPartition, ::Type{T}, ::Type{S}, R::DataType...) w
8181
ArrayPartition(f, N)
8282
end
8383

84-
Base.copy(A::ArrayPartition{T, S}) where {T, S} = ArrayPartition{T, S}(copy.(A.x))
84+
Base.copy(A::ArrayPartition) = ArrayPartition(map(copy, A.x))
8585

8686
## zeros
87-
Base.zero(A::ArrayPartition{T, S}) where {T, S} = ArrayPartition{T, S}(zero.(A.x))
87+
Base.zero(A::ArrayPartition) = ArrayPartition(map(zero, A.x))
8888
# ignore dims since array partitions are vectors
8989
Base.zero(A::ArrayPartition, dims::NTuple{N, Int}) where {N} = zero(A)
9090

test/partitions_test.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ x = ArrayPartition([1, 2], [3.0, 4.0])
6969
@test (@inferred similar(x, Int, (2, 2))) isa AbstractMatrix{Int}
7070
# @inferred similar(x, Int, Float64)
7171

72+
# Copy
73+
@inferred copy(x)
74+
@inferred copy(ArrayPartition(x, x))
75+
7276
# zero
7377
@inferred zero(x)
7478
@inferred zero(x, (2, 2))
7579
@inferred zero(x)
80+
@inferred zero(ArrayPartition(x, x))
7681

7782
# ones
7883
@inferred ones(x)
@@ -225,3 +230,15 @@ begin
225230
@test convert(new_type, d) == d
226231
@test_throws MethodError convert(new_type, ArrayPartition(view(b, :), c, c))
227232
end
233+
234+
235+
@testset "Copy and zero with type changing array" begin
236+
# Motivating use case for this is ArrayPartitions of Arrow arrays which are mmap:ed and change type when copied
237+
struct TypeChangingArray{T, N} <: AbstractArray{T, N} end
238+
Base.copy(::TypeChangingArray{T, N}) where {T,N} = Array{T,N}(undef, ntuple(_ -> 0, N))
239+
Base.zero(::TypeChangingArray{T, N}) where {T,N} = zeros(T, ntuple(_ -> 0, N))
240+
241+
a = ArrayPartition(TypeChangingArray{Int, 2}(), TypeChangingArray{Float32, 2}())
242+
@test copy(a) == ArrayPartition(zeros(Int, 0, 0), zeros(Float32, 0, 0))
243+
@test zero(a) == ArrayPartition(zeros(Int, 0, 0), zeros(Float32, 0, 0))
244+
end

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ using Test
44
using Aqua
55
using SafeTestsets
66

7-
Aqua.test_all(RecursiveArrayTools, ambiguities = false)
7+
if VERSION >= v"1.9"
8+
Aqua.test_all(RecursiveArrayTools, ambiguities = false)
9+
end
10+
811
@test_broken isempty(Test.detect_ambiguities(RecursiveArrayTools))
912
const GROUP = get(ENV, "GROUP", "All")
1013
const is_APPVEYOR = (Sys.iswindows() && haskey(ENV, "APPVEYOR"))

0 commit comments

Comments
 (0)