Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conversions in tuple's #27

Open
green-nsk opened this issue Nov 8, 2022 · 0 comments
Open

Support conversions in tuple's #27

green-nsk opened this issue Nov 8, 2022 · 0 comments

Comments

@green-nsk
Copy link
Contributor

Conversions API currently doesn't look inside tuples.

using LightBSON

struct X
    x::String
end

function Base.setindex!(writer::BSONWriter, value::X, index::Union{String, Symbol})
    GC.@preserve value setindex!(writer, UnsafeBSONString(pointer(value.x), length(value.x)), index)
end

LightBSON.bson_representation_type(::Type{X}) = UnsafeBSONString

buf = UInt8[]; writer = BSONWriter(buf); writer["x"] = X("foo"); close(writer)
reader = BSONReader(buf); reader["x"][X]

However this throws:

buf = UInt8[]; writer = BSONWriter(buf); writer["x"] = (X("foo"),); close(writer)
reader = BSONReader(buf); reader["x"][Tuple{X}]
ERROR: MethodError: Cannot `convert` an object of type String to an object of type X
Closest candidates are:
  convert(::Type{T}, ::T) where T at ~/distr/julia-1.7.3/share/julia/base/essentials.jl:218
  X(::String) at ~/code/scratch.jl:4
  X(::Any) at ~/code/scratch.jl:4
Stacktrace:
 [1] _totuple
   @ ./tuple.jl:330 [inlined]
 [2] Tuple{X}(itr::Vector{Any})
   @ Base ./tuple.jl:317
 [3] bson_representation_convert
   @ ~/.julia/packages/LightBSON/WrSYn/src/representations.jl:14 [inlined]

Defining Base.convert(::Type{X}, ::AbstractString) helps, but but it shouldn't be needed:

Base.convert(::Type{X}, v::AbstractString) = X(v)
reader = BSONReader(buf); reader["x"][Tuple{X}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant