Skip to content

Commit ea14d86

Browse files
jlchanpiever
andauthored
Revert FieldArray behavior to pre-0.6 (#207)
* restricting StaticArrays schema to SArray instead of <:StaticArray * add test * use `invoke` for FieldArray types * add test * specializing StructArrays.components for FieldArray * adding more tests * Update src/staticarrays_support.jl Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com> * add createinstance * edit FlippedVec2D tuple constructor Co-authored-by: Jesse Chan <jesse.chan@rice.edu> Co-authored-by: Pietro Vertechi <pietro.vertechi@protonmail.com>
1 parent f3bef32 commit ea14d86

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/staticarrays_support.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import StaticArrays: StaticArray, tuple_prod
1+
import StaticArrays: StaticArray, FieldArray, tuple_prod
22

33
"""
44
StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T}
@@ -8,6 +8,9 @@ The `staticschema` of a `StaticArray` element type is the `staticschema` of the
88
julia> StructArrays.staticschema(SVector{2, Float64})
99
Tuple{Float64, Float64}
1010
```
11+
The one exception to this rule is `<:StaticArrays.FieldArray`, since `FieldArray` is based on a
12+
struct. In this case, `staticschema(<:FieldArray)` returns the `staticschema` for the struct
13+
which subtypes `FieldArray`.
1114
"""
1215
@generated function StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T}
1316
return quote
@@ -17,3 +20,10 @@ Tuple{Float64, Float64}
1720
end
1821
StructArrays.createinstance(::Type{T}, args...) where {T<:StaticArray} = T(args)
1922
StructArrays.component(s::StaticArray, i) = getindex(s, i)
23+
24+
# invoke general fallbacks for a `FieldArray` type.
25+
@inline function StructArrays.staticschema(T::Type{<:FieldArray})
26+
invoke(StructArrays.staticschema, Tuple{Type{<:Any}}, T)
27+
end
28+
StructArrays.component(s::FieldArray, i) = invoke(StructArrays.component, Tuple{Any, Any}, s, i)
29+
StructArrays.createinstance(T::Type{<:FieldArray}, args...) = invoke(createinstance, Tuple{Type{<:Any}, Vararg}, T, args...)

test/runtests.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,26 @@ end
852852
@test x .+ y == StructArray([StaticArrayType{Tuple{1,2}}(3*ones(1,2) .+ 2*i) for i = 0:1])
853853
end
854854

855+
# test FieldVector constructor (see https://github.com/JuliaArrays/StructArrays.jl/issues/205)
856+
struct FlippedVec2D <: FieldVector{2,Float64}
857+
x::Float64
858+
y::Float64
859+
end
860+
# tuple constructors should respect the flipped ordering
861+
FlippedVec2D(t::Tuple) = FlippedVec2D(t[2], t[1])
862+
863+
# define a custom getindex to test StructArrays.component(::FieldArray) behavior
864+
Base.getindex(a::FlippedVec2D, index::Int) = index==1 ? a.y : a.x
865+
Base.Tuple(a::FlippedVec2D) = (a.y, a.x)
866+
a = StructArray([FlippedVec2D(1.0,2.0)])
867+
@test a.x == [1.0]
868+
@test a.y == [2.0]
869+
@test a.x[1] == a[1].x
870+
871+
# test custom indices and components
872+
@test typeof(StructArrays.components(a)) == NamedTuple{(:x, :y), NTuple{2, Vector{Float64}}}
873+
@test StructArrays.components(a) == (; x = [1.0], y = [2.0])
874+
855875
# test type stability of creating views with "many" homogeneous components
856876
for n in 1:10
857877
u = StructArray(randn(SVector{n, Float64}) for _ in 1:10, _ in 1:5)

0 commit comments

Comments
 (0)