Skip to content

Commit

Permalink
Deprecate Base.convert(IOStream, ::Message)
Browse files Browse the repository at this point in the history
This had two problems:
- It actually returned an `IOBuffer`.
- It made a (potentially) unnecessary copy of the `Message`.
  • Loading branch information
JamesWrigley committed Jul 20, 2024
1 parent e66c836 commit 8e53a91
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/src/_changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Changelog](https://keepachangelog.com).
1.11 ([#244]).
- Full [Bindings](@ref) to libzmq ([#232]).

### Deprecated
- The `Base.convert(IOStream, ::Message)` method has been deprecated due to
buggy behaviour, use `IOBuffer(msg)` instead ([#247]).

### Fixed
- Fixed [`isfreed()`](@ref), which would previously return the wrong values
([#245]).
Expand Down
27 changes: 25 additions & 2 deletions src/message.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,31 @@ function gc_free_fn(data::Ptr{Cvoid}, hint::Ptr{Cvoid})
end

"""
High-level Message object for sending/receiving ZMQ messages in shared buffers.
mutable struct Message <: AbstractArray{UInt8, 1}
High-level `Message` object for sending/receiving ZMQ messages in shared
buffers. As an `AbstractArray`, it supports common (non-resizeable) array
behaviour.
# Examples
```jldoctest
julia> using ZMQ
julia> m = Message("foo");
julia> Char(m[1]) # Array indexing
'f': ASCII/Unicode U+0066 (category Ll: Letter, lowercase)
julia> m[end] = Int('g'); # Array assignment
julia> unsafe_string(m) # Conversion to string (only do this if you know the message is a string)
"fog"
julia> IOBuffer(m) # Create a zero-copy IOBuffer
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=3, maxsize=Inf, ptr=1, mark=-1)
```
"""
mutable struct Message <: AbstractArray{UInt8,1}
mutable struct Message <: AbstractArray{UInt8, 1}
# Matching the declaration in the header: char _[64];
w_padding::_Message
handle::Ptr{Cvoid} # index into gc_protect, if any
Expand Down Expand Up @@ -164,6 +186,7 @@ Base.strides(::Message) = (1,)
# Build an IOStream from a message
# Copies the data
function Base.convert(::Type{IOStream}, zmsg::Message)
Base.depwarn("convert(IOStream, ::Message) is deprecated, use `IOBuffer(zmsg)` instead to make a zero-copy IOBuffer from a Message.", :convert)
s = IOBuffer()
write(s, zmsg)
return s
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ end

ZMQ.send(s2, Message("another test request"))
msg = ZMQ.recv(s1)
o=convert(IOStream, msg)
seek(o, 0)
o = IOBuffer(msg)
@test String(take!(o)) == "another test request"
ZMQ.send(s1) do io
print(io, "buffer ")
Expand Down Expand Up @@ -218,6 +217,7 @@ end
@test !Bool(m.more)
@test_throws ErrorException m.more = true
@test ZMQ.isfreed(m)
@test_logs (:warn, r"convert(.+) is deprecated") convert(IOStream, m)
end

@testset "ZMQ resource management" begin
Expand Down

0 comments on commit 8e53a91

Please sign in to comment.