diff --git a/src/structarray.jl b/src/structarray.jl index 496604e0..89f1c2cd 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -368,6 +368,12 @@ end StructArray{T}(map(v -> @inbounds(view(v, I...)), components(s))) end +function Base.parentindices(s::StructArray) + res = parentindices(component(s, 1)) + all(c -> parentindices(c) == res, components(s)) || throw(ArgumentError("inconsistent parentindices of components")) + return res +end + Base.@propagate_inbounds function Base.setindex!(s::StructArray{T, <:Any, <:Any, CartesianIndex{N}}, vals, I::Vararg{Int, N}) where {T,N} @boundscheck checkbounds(s, I...) valsT = maybe_convert_elt(T, vals) diff --git a/test/runtests.jl b/test/runtests.jl index 347438d9..852aec6d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -37,6 +37,8 @@ Base.convert(::Type{Millimeters}, x::Meters) = Millimeters(x.x*1000) @test (@inferred t[2,1:2]) == StructArray((a = [3, 4], b = [6, 7])) @test_throws BoundsError t[3,3] @test (@inferred view(t, 2, 1:2)) == StructArray((a = view(a, 2, 1:2), b = view(b, 2, 1:2))) + @test @inferred(parentindices(view(t, 2, 1:2))) == (2, 1:2) + @test_throws ArgumentError parentindices(StructArray((view([1, 2], [1]), view([1, 2], [2])))) # Element type conversion (issue #216) x = StructArray{Complex{Int}}((a, b))