Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const EmptyTup = Union{Tuple{}, NamedTuple{(), Tuple{}}}
Default to `getfield`. It should be overloaded for custom types with a custom
schema. See [`StructArrays.staticschema`](@ref).
"""
component(x, i) = getfield(x, i)
@generated component(x::T, i) where {T} = fieldcount(T) == 0 ? :(x) : :(getfield(x, i))

"""
StructArrays.staticschema(T)
Expand All @@ -26,9 +26,13 @@ NamedTuple{(:re, :im),Tuple{Float64,Float64}}
```
"""
@generated function staticschema(::Type{T}) where {T}
name_tuple = Expr(:tuple, [QuoteNode(f) for f in fieldnames(T)]...)
type_tuple = Expr(:curly, :Tuple, [Expr(:call, :fieldtype, :T, i) for i in 1:fieldcount(T)]...)
Expr(:curly, :NamedTuple, name_tuple, type_tuple)
if fieldcount(T) == 0
:(Tuple{T})
else
name_tuple = Expr(:tuple, [QuoteNode(f) for f in fieldnames(T)]...)
type_tuple = Expr(:curly, :Tuple, [Expr(:call, :fieldtype, :T, i) for i in 1:fieldcount(T)]...)
Expr(:curly, :NamedTuple, name_tuple, type_tuple)
end
end

staticschema(::Type{T}) where {T<:Tup} = T
Expand Down
2 changes: 0 additions & 2 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,7 @@ staticschema(::Type{StructArray{T, N, C, I}}) where {T, N, C, I} = staticschema(
createinstance(::Type{<:StructArray{T}}, args...) where {T} = StructArray{T}(args)

Base.size(s::StructArray) = size(components(s)[1])
Base.size(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (0,)
Base.axes(s::StructArray) = axes(components(s)[1])
Base.axes(s::StructArray{<:Any, <:Any, <:EmptyTup}) = (1:0,)

"""
StructArrays.get_ith(cols::Union{Tuple,NamedTuple}, I...)
Expand Down
12 changes: 8 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,14 @@ Create an instance of type `T` from a tuple of field values `args`, bypassing
possible internal constructors. `T` should be a concrete type.
"""
@generated function bypass_constructor(::Type{T}, args) where {T}
vars = ntuple(_ -> gensym(), fieldcount(T))
assign = [:($var::$(fieldtype(T, i)) = getfield(args, $i)) for (i, var) in enumerate(vars)]
construct = Expr(:new, :T, vars...)
Expr(:block, assign..., construct)
if fieldcount(T) == 0
:(only(args))
else
vars = ntuple(_ -> gensym(), fieldcount(T))
assign = [:($var::$(fieldtype(T, i)) = getfield(args, $i)) for (i, var) in enumerate(vars)]
construct = Expr(:new, :T, vars...)
Expr(:block, assign..., construct)
end
end

# Specialize this for types like LazyRow that shouldn't convert
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ end
t = StructVector([(a=1,), (a=missing,)])::StructVector
@test isequal(t.a, [1, missing])
@test eltype(t) <: NamedTuple{(:a,)}

@test StructArray([nothing])::StructArray == [nothing]
@test StructArray(Nothing[])::StructArray == Nothing[]
@test StructArray(1:3)::StructArray == 1:3
@test StructArray(Int[])::StructArray == Int[]
@test_broken StructArray([])::StructArray == []
end

@testset "tuple case" begin
Expand Down