Skip to content

Commit b7c9f89

Browse files
author
Pietro Vertechi
committed
make IndexStyle compatible with Base and test inferrability
1 parent 3b5d398 commit b7c9f89

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/structarray.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ const StructVector{T, C<:NamedTuple} = StructArray{T, 1, C}
4242
StructVector{T}(args...; kwargs...) where {T} = StructArray{T}(args...; kwargs...)
4343
StructVector(args...; kwargs...) = StructArray(args...; kwargs...)
4444

45-
Base.IndexStyle(::Type{StructArray{T, N, C}}) where {T, N, C} = Base.IndexStyle(tuple_type(C).parameters[1])
45+
_indexstyle(::Type{Tuple{}}) = IndexStyle(Union{})
46+
_indexstyle(::Type{T}) where {T<:Tuple} = IndexStyle(IndexStyle(tuple_type_head(T)), _indexstyle(tuple_type_tail(T)))
47+
_indexstyle(::Type{NamedTuple{names, types}}) where {names, types} = _indexstyle(types)
48+
49+
function Base.IndexStyle(::Type{StructArray{T, N, C}}) where {T, N, C}
50+
_indexstyle(C)
51+
end
4652

4753
_undef_array(::Type{T}, sz; unwrap = t -> false) where {T} = unwrap(T) ? StructArray{T}(undef, sz; unwrap = unwrap) : Array{T}(undef, sz)
4854

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ end
1717
@test StructArrays.propertynames(StructArrays.fieldarrays(t)) == (:a, :b)
1818
end
1919

20+
@testset "indexstyle" begin
21+
style1 = @inferred IndexStyle(StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10)))
22+
@test style1 isa IndexCartesian
23+
style2 = @inferred IndexStyle(StructArray(a=rand(10,10), b=rand(10,10)))
24+
@test style2 isa IndexLinear
25+
end
26+
2027
@testset "replace_storage" begin
2128
v = StructArray(a=rand(10), b = fill("string", 10))
2229
v_pooled = StructArrays.replace_storage(v) do c

0 commit comments

Comments
 (0)