Skip to content

Commit da39dc6

Browse files
authored
Merge pull request #247 from JuliaInterop/iostream
Deprecate `Base.convert(IOStream, ::Message)`
2 parents e66c836 + 8e53a91 commit da39dc6

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

docs/src/_changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Changelog](https://keepachangelog.com).
1414
1.11 ([#244]).
1515
- Full [Bindings](@ref) to libzmq ([#232]).
1616

17+
### Deprecated
18+
- The `Base.convert(IOStream, ::Message)` method has been deprecated due to
19+
buggy behaviour, use `IOBuffer(msg)` instead ([#247]).
20+
1721
### Fixed
1822
- Fixed [`isfreed()`](@ref), which would previously return the wrong values
1923
([#245]).

src/message.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,31 @@ function gc_free_fn(data::Ptr{Cvoid}, hint::Ptr{Cvoid})
2323
end
2424

2525
"""
26-
High-level Message object for sending/receiving ZMQ messages in shared buffers.
26+
mutable struct Message <: AbstractArray{UInt8, 1}
27+
28+
High-level `Message` object for sending/receiving ZMQ messages in shared
29+
buffers. As an `AbstractArray`, it supports common (non-resizeable) array
30+
behaviour.
31+
32+
# Examples
33+
```jldoctest
34+
julia> using ZMQ
35+
36+
julia> m = Message("foo");
37+
38+
julia> Char(m[1]) # Array indexing
39+
'f': ASCII/Unicode U+0066 (category Ll: Letter, lowercase)
40+
41+
julia> m[end] = Int('g'); # Array assignment
42+
43+
julia> unsafe_string(m) # Conversion to string (only do this if you know the message is a string)
44+
"fog"
45+
46+
julia> IOBuffer(m) # Create a zero-copy IOBuffer
47+
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=3, maxsize=Inf, ptr=1, mark=-1)
48+
```
2749
"""
28-
mutable struct Message <: AbstractArray{UInt8,1}
50+
mutable struct Message <: AbstractArray{UInt8, 1}
2951
# Matching the declaration in the header: char _[64];
3052
w_padding::_Message
3153
handle::Ptr{Cvoid} # index into gc_protect, if any
@@ -164,6 +186,7 @@ Base.strides(::Message) = (1,)
164186
# Build an IOStream from a message
165187
# Copies the data
166188
function Base.convert(::Type{IOStream}, zmsg::Message)
189+
Base.depwarn("convert(IOStream, ::Message) is deprecated, use `IOBuffer(zmsg)` instead to make a zero-copy IOBuffer from a Message.", :convert)
167190
s = IOBuffer()
168191
write(s, zmsg)
169192
return s

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ end
121121

122122
ZMQ.send(s2, Message("another test request"))
123123
msg = ZMQ.recv(s1)
124-
o=convert(IOStream, msg)
125-
seek(o, 0)
124+
o = IOBuffer(msg)
126125
@test String(take!(o)) == "another test request"
127126
ZMQ.send(s1) do io
128127
print(io, "buffer ")
@@ -218,6 +217,7 @@ end
218217
@test !Bool(m.more)
219218
@test_throws ErrorException m.more = true
220219
@test ZMQ.isfreed(m)
220+
@test_logs (:warn, r"convert(.+) is deprecated") convert(IOStream, m)
221221
end
222222

223223
@testset "ZMQ resource management" begin

0 commit comments

Comments
 (0)