Skip to content
Merged
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
14 changes: 1 addition & 13 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ function buildfromschema(initializer, ::Type{T}, ::Type{NT}) where {T, NT<:Tup}
StructArray{T}(nt)
end

Base.@pure SkipConstructor(::Type) = false

@static if VERSION < v"1.2.0"
@inline _getproperty(v::Tuple, field) = getfield(v, field)
@inline _getproperty(v, field) = getproperty(v, field)
Expand All @@ -48,19 +46,9 @@ end

foreachfield(f, x::T, xs...) where {T} = foreachfield(staticschema(T), f, x, xs...)

function createinstance(::Type{T}, args...) where {T}
SkipConstructor(T) ? unsafe_createinstance(T, args...) : T(args...)
end

createinstance(::Type{T}, args...) where {T} = T(args...)
createinstance(::Type{T}, args...) where {T<:Union{Tuple, NamedTuple}} = T(args)

@generated function unsafe_createinstance(::Type{T}, args...) where {T}
v = fieldnames(T)
new_tup = Expr(:(=), Expr(:tuple, v...), :args)
construct = Expr(:new, :T, (:(convert(fieldtype(T, $(Expr(:quote, sym))), $sym)) for sym in v)...)
Expr(:block, new_tup, construct)
end

add_params(::Type{T}, ::Type{C}) where {T, C<:Tuple} = T
add_params(::Type{T}, ::Type{C}) where {T<:Tuple, C<:Tuple} = C
add_params(::Type{<:NamedTuple{names}}, ::Type{C}) where {names, C<:Tuple} = NamedTuple{names, C}
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ struct S
S(x) = new(x, x)
end

StructArrays.SkipConstructor(::Type{<:S}) = true
StructArrays.createinstance(::Type{<:S}, x, y) = S(x)

@testset "inner" begin
v = StructArray{S}(([1], [1]))
Expand Down