Skip to content

Commit

Permalink
WeakRefStringArray: make eltype respect Missing
Browse files Browse the repository at this point in the history
enhances #17 so that WeakRefStringArray allows missing iff
the underlying WeakRefString Array allows missing
  • Loading branch information
alyst committed Dec 20, 2017
1 parent 744f4eb commit 45d595d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/WeakRefStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ Base.convert(::Type{String}, x::WeakRefString) = convert(String, string(x))
Base.String(x::WeakRefString) = string(x)
Base.Symbol(x::WeakRefString{UInt8}) = ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int), x.ptr, x.len)

struct WeakRefStringArray{T, N} <: AbstractArray{Union{String, Missing}, N}
struct WeakRefStringArray{T, N, U} <: AbstractArray{Union{String, U}, N}
data::Vector{Any}
elements::Array{T, N}
elements::Array{Union{T, U}, N}

WeakRefStringArray(data::Vector{UInt8}, ::Type{T}, rows::Integer) where {T <: WeakRefString} =
new{T, 1, Union{}}(Any[data], zeros(T, rows))
WeakRefStringArray(data::Vector{UInt8}, ::Type{Union{T, Missing}}, rows::Integer) where {T <: WeakRefString} =
new{T, 1, Missing}(Any[data], Vector{Union{T, Missing}}(rows))
WeakRefStringArray(data::Vector{UInt8}, A::Array{T, N}) where {T <: Union{WeakRefString, Missing}, N} =
new{Missings.T(T), N, T >: Missing ? Missing : Union{}}(Any[data], A)
end

WeakRefStringArray(data::Vector{UInt8}, ::Type{T}, rows::Integer) where {T <: WeakRefString} = WeakRefStringArray(Any[data], zeros(T, rows))
WeakRefStringArray(data::Vector{UInt8}, ::Type{Union{Missing, T}}, rows::Integer) where {T} = WeakRefStringArray(Any[data], Vector{Union{Missing, T}}(rows))
WeakRefStringArray(data::Vector{UInt8}, A::Array{T}) where {T <: Union{WeakRefString, Missing}} = WeakRefStringArray(Any[data], A)

wk(w::WeakRefString) = string(w)
wk(::Missing) = missing

Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ end
str = WeakRefString(pointer(data), 3)
C = WeakRefStringArray(data, [str])
@test size(C) == (1,)
@test eltype(C) === String

A = WeakRefStringArray(UInt8[], WeakRefString{UInt8}, 10)
@test size(A) == (10,)
Expand All @@ -72,6 +73,8 @@ end
B[1] = "ho"
append!(A, B)
@test size(A) == (17,)
@test_throws MethodError setindex!(B, missing, 1)
@test_throws MethodError push!(B, missing)

D = WeakRefStringArray(UInt8[], Union{Missing, WeakRefString{UInt8}}, 0)
push!(D, "hey")
Expand All @@ -82,4 +85,10 @@ end
@test D[3] === missing
D[2] = missing
@test D[2] === missing
@test_throws MethodError append!(A, D)
@test_broken size(A) == (17,) # append!() changes the size of A

E = WeakRefStringArray(data, [str missing])
@test size(E) == (1, 2)
@test eltype(E) === Union{String, Missing}
end

0 comments on commit 45d595d

Please sign in to comment.