Skip to content

Commit b94e242

Browse files
committed
change read(::IO, ::Ref) to read!`, fix fix #21592
deprecate `read(io, type, dims)`, fix #21450
1 parent 259996c commit b94e242

File tree

6 files changed

+18
-21
lines changed

6 files changed

+18
-21
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ Deprecated or removed
136136
* The method `replace(s::AbstractString, pat, r, count)` with `count <= 0` is deprecated
137137
in favor of `replace(s::AbstractString, pat, r, typemax(Int))` ([#22325]).
138138

139+
* `read(io, type, dims)` is deprecated to `read!(io, Array{type}(dims))` ([#21450]).
140+
141+
* `read(::IO, ::Ref)` is now a method of `read!`, since it mutates its `Ref` argument ([#21592]).
142+
139143

140144
Julia v0.6.0 Release Notes
141145
==========================

base/deprecated.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,12 @@ end
15431543
@deprecate cat_t{N,T}(::Type{Val{N}}, ::Type{T}, A, B) cat_t(Val(N), T, A, B) false
15441544
@deprecate reshape{N}(A::AbstractArray, ::Type{Val{N}}) reshape(A, Val(N))
15451545

1546+
@deprecate read(s::IO, x::Ref) read!(s, x)
1547+
1548+
@deprecate read(s::IO, t::Type, d1::Int, dims::Int...) read!(s, Array{t}(tuple(d1,dims...)))
1549+
@deprecate read(s::IO, t::Type, d1::Integer, dims::Integer...) read!(s, Array{t}(convert(Tuple{Vararg{Int}},tuple(d1,dims...))))
1550+
@deprecate read(s::IO, t::Type, dims::Dims) read!(s, Array{t}(dims))
1551+
15461552
# END 0.7 deprecations
15471553

15481554
# BEGIN 1.0 deprecations

base/distributed/messages.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function serialize_hdr_raw(io, hdr)
167167
end
168168

169169
function deserialize_hdr_raw(io)
170-
data = read(io, Ref{NTuple{4,Int}}())[]
170+
data = read!(io, Ref{NTuple{4,Int}}())[]
171171
return MsgHeader(RRID(data[1], data[2]), RRID(data[3], data[4]))
172172
end
173173

base/io.jl

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -368,29 +368,16 @@ end
368368

369369
@noinline unsafe_read(s::IO, p::Ref{T}, n::Integer) where {T} = unsafe_read(s, unsafe_convert(Ref{T}, p)::Ptr, n) # mark noinline to ensure ref is gc-rooted somewhere (by the caller)
370370
unsafe_read(s::IO, p::Ptr, n::Integer) = unsafe_read(s, convert(Ptr{UInt8}, p), convert(UInt, n))
371-
read(s::IO, x::Ref{T}) where {T} = (unsafe_read(s, x, Core.sizeof(T)); x)
371+
read!(s::IO, x::Ref{T}) where {T} = (unsafe_read(s, x, Core.sizeof(T)); x)
372372

373373
read(s::IO, ::Type{Int8}) = reinterpret(Int8, read(s, UInt8))
374374
function read(s::IO, T::Union{Type{Int16},Type{UInt16},Type{Int32},Type{UInt32},Type{Int64},Type{UInt64},Type{Int128},Type{UInt128},Type{Float16},Type{Float32},Type{Float64}})
375-
return read(s, Ref{T}(0))[]::T
375+
return read!(s, Ref{T}(0))[]::T
376376
end
377377

378378
read(s::IO, ::Type{Bool}) = (read(s, UInt8) != 0)
379379
read(s::IO, ::Type{Ptr{T}}) where {T} = convert(Ptr{T}, read(s, UInt))
380380

381-
read(s::IO, t::Type{T}, d1::Int, dims::Int...) where {T} = read(s, t, tuple(d1,dims...))
382-
read(s::IO, t::Type{T}, d1::Integer, dims::Integer...) where {T} =
383-
read(s, t, convert(Tuple{Vararg{Int}},tuple(d1,dims...)))
384-
385-
"""
386-
read(stream::IO, T, dims)
387-
388-
Read a series of values of type `T` from `stream`, in canonical binary representation.
389-
`dims` is either a tuple or a series of integer arguments specifying the size of the `Array{T}`
390-
to return.
391-
"""
392-
read(s::IO, ::Type{T}, dims::Dims) where {T} = read!(s, Array{T}(dims))
393-
394381
@noinline function read!(s::IO, a::Array{UInt8}) # mark noinline to ensure the array is gc-rooted somewhere (by the caller)
395382
unsafe_read(s, pointer(a), sizeof(a))
396383
return a
@@ -530,7 +517,7 @@ end
530517
531518
Read at most `nb` bytes from `s`, returning a `Vector{UInt8}` of the bytes read.
532519
"""
533-
function read(s::IO, nb=typemax(Int))
520+
function read(s::IO, nb::Integer = typemax(Int))
534521
# Let readbytes! grow the array progressively by default
535522
# instead of taking of risk of over-allocating
536523
b = Vector{UInt8}(nb == typemax(Int) ? 1024 : nb)

base/serialize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ function deserialize_array(s::AbstractSerializer)
863863
end
864864
end
865865
else
866-
A = read(s.io, elty, dims)
866+
A = read!(s.io, Array{elty}(dims))
867867
end
868868
s.table[slot] = A
869869
return A

test/read.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ for (name, f) in l
149149
@test read(io(), Int) == read(filename,Int)
150150
s1 = io()
151151
s2 = IOBuffer(text)
152-
@test read(s1, UInt32, 2) == read(s2, UInt32, 2)
152+
@test read!(s1, Array{UInt32}(2)) == read!(s2, Array{UInt32}(2))
153153
@test !eof(s1)
154-
@test read(s1, UInt8, 5) == read(s2, UInt8, 5)
154+
@test read!(s1, Array{UInt8}(5)) == read!(s2, Array{UInt8}(5))
155155
@test !eof(s1)
156-
@test read(s1, UInt8, 1) == read(s2, UInt8, 1)
156+
@test read!(s1, Array{UInt8}(1)) == read!(s2, Array{UInt8}(1))
157157
@test eof(s1)
158158
@test_throws EOFError read(s1, UInt8)
159159
@test eof(s1)

0 commit comments

Comments
 (0)