Unable to add in support for Complex values as expanded into arrays

Open
#52 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
julia
Domain
backend

Research direction

Reproduce the reported ComplexF64 round trip using the packing and unpacking calls in the issue. Start in src/unpack.jl around the ArrayType and invalid_unpack paths named in the stack trace, then trace the type-driven dispatch for ComplexF64. Done means typed unpacking accepts the encoded two-element float array and preserves the demonstrated MessagePack representation.

Written by the indexing model from the issue text.

Description

I'm trying to serialize and deserialize complex values in a similar fashion to how it is done with Rust's num-complex + rmp-serde packages

  • expanding ComplexF32 to an array of Float32
  • expanding ComplexF64 to an array of Float64

As an example, num-complex in Rust will generate the following bytes for 1.1 + 0.1im:
[0x92, 0xcb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xcb, 0x3f, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a]

I'm trying to do the same with Julia with the following piece of code

MsgPack.msgpack_type(::Type{Complex{Float32}}) = MsgPack.ArrayType()
MsgPack.msgpack_type(::Type{Complex{Float64}}) = MsgPack.ArrayType()
MsgPack.to_msgpack(::MsgPack.ArrayType, arr::Complex{Float32}) = [real(arr), imag(arr)]
MsgPack.to_msgpack(::MsgPack.ArrayType, arr::Complex{Float64}) = [real(arr), imag(arr)]
MsgPack.from_msgpack(::Type{Complex{Float32}}, arr::Vector{Float32}) = Complex{Float32}(arr[1], arr[2])
MsgPack.from_msgpack(::Type{Complex{Float64}}, arr::Vector{Float64}) = Complex{Float64}(arr[1], arr[2])

I can successfully generate the same bytes with this

julia> val
1.1 + 0.1im
julia> pack(val)
19-element Vector{UInt8}:
 0x92
 0xcb
.... (more stuff here)
julia> unpack(pack(val))
2-element Vector{Any}:
 1.1
 0.1

but unpack(pack(val), ComplexF64) fails with this

julia> unpack(pack(val), ComplexF64)
ERROR: invalid byte 0xcb encountered in IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=19, maxsize=Inf, ptr=3, mark=-1) attempting to read a MsgPack MsgPack.ArrayType() into a Julia ComplexF64 at position 2
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] invalid_unpack(io::IOBuffer, byte::UInt8, m::MsgPack.ArrayType, T::Type)
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:543
  [3] unpack_type(io::IOBuffer, byte::UInt8, t::MsgPack.ArrayType, ::Type{ComplexF64}; strict::Tuple{})
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:396
  [4] _unpack_array(io::IOBuffer, n::UInt8, #unused#::Type{ComplexF64}, strict::Tuple{})
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:411
  [5] unpack_format(io::IOBuffer, f::MsgPack.ArrayFixFormat, #unused#::Type{ComplexF64}, strict::Tuple{})
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:402
  [6] unpack_type(io::IOBuffer, byte::UInt8, t::MsgPack.ArrayType, ::Type{ComplexF64}; strict::Tuple{})
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:390
  [7] unpack(io::IOBuffer, ::Type{ComplexF64}; strict::Tuple{})
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:32
  [8] unpack(bytes::Vector{UInt8}, ::Type{ComplexF64}; strict::Tuple{})
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:8
  [9] unpack(bytes::Vector{UInt8}, ::Type{ComplexF64})
    @ MsgPack ~/.julia/packages/MsgPack/pN9xd/src/unpack.jl:8
 [10] top-level scope
    @ REPL[46]:1

What am I doing wrong here?

Dominant language
Julia
Stars
68
Forks
20
Avg merge
13d 12h
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaIO/MsgPack.jl

All issues in JuliaIO/MsgPack.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.