From e3891d204fa5fb13243c4034ed95bda3054e15bd Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 20 Jun 2017 19:32:55 -0700 Subject: [PATCH] change `read(::IO, ::Ref) to `read!`, fix fix #21592 deprecate `read(io, type, dims)`, fix #21450 --- NEWS.md | 4 ++++ base/deprecated.jl | 6 ++++++ base/distributed/messages.jl | 2 +- base/io.jl | 19 +++---------------- base/serialize.jl | 2 +- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6bcc2fc706f35b..d708606aa9bc4e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -110,6 +110,10 @@ Deprecated or removed implementations is now in AbstractFFTs.jl, the bindings to the FFTW library are in FFTW.jl, and the Base signal processing functions which used FFTs are now in DSP.jl ([#21956]). + * `read(io, type, dims)` is deprecated to `read!(io, Array{type}(dims))` ([#21450]). + + * `read(::IO, ::Ref)` is now a method of `read!`, since it mutates its `Ref` argument ([#21592]). + Julia v0.6.0 Release Notes ========================== diff --git a/base/deprecated.jl b/base/deprecated.jl index c47894dce94f40..08731ea5c63832 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1485,6 +1485,12 @@ end using .DSP export conv, conv2, deconv, filt, filt!, xcorr +@deprecate read(s::IO, x::Ref) read!(s, x) + +@deprecate read(s::IO, t::Type, d1::Int, dims::Int...) read!(s, Array{t}(tuple(d1,dims...))) +@deprecate read(s::IO, t::Type, d1::Integer, dims::Integer...) read!(s, Array{t}(convert(Tuple{Vararg{Int}},tuple(d1,dims...)))) +@deprecate read(s::IO, t::Type, dims::Dims) read!(s, Array{t}(dims)) + # END 0.7 deprecations # BEGIN 1.0 deprecations diff --git a/base/distributed/messages.jl b/base/distributed/messages.jl index 17a447a154d1fa..222c38c9ba1bae 100644 --- a/base/distributed/messages.jl +++ b/base/distributed/messages.jl @@ -167,7 +167,7 @@ function serialize_hdr_raw(io, hdr) end function deserialize_hdr_raw(io) - data = read(io, Ref{NTuple{4,Int}}())[] + data = read!(io, Ref{NTuple{4,Int}}())[] return MsgHeader(RRID(data[1], data[2]), RRID(data[3], data[4])) end diff --git a/base/io.jl b/base/io.jl index 244165b37c73db..b72ea629c4e1e0 100644 --- a/base/io.jl +++ b/base/io.jl @@ -361,29 +361,16 @@ end @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) unsafe_read(s::IO, p::Ptr, n::Integer) = unsafe_read(s, convert(Ptr{UInt8}, p), convert(UInt, n)) -read(s::IO, x::Ref{T}) where {T} = (unsafe_read(s, x, Core.sizeof(T)); x) +read!(s::IO, x::Ref{T}) where {T} = (unsafe_read(s, x, Core.sizeof(T)); x) read(s::IO, ::Type{Int8}) = reinterpret(Int8, read(s, UInt8)) 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}}) - return read(s, Ref{T}(0))[]::T + return read!(s, Ref{T}(0))[]::T end read(s::IO, ::Type{Bool}) = (read(s, UInt8) != 0) read(s::IO, ::Type{Ptr{T}}) where {T} = convert(Ptr{T}, read(s, UInt)) -read(s::IO, t::Type{T}, d1::Int, dims::Int...) where {T} = read(s, t, tuple(d1,dims...)) -read(s::IO, t::Type{T}, d1::Integer, dims::Integer...) where {T} = - read(s, t, convert(Tuple{Vararg{Int}},tuple(d1,dims...))) - -""" - read(stream::IO, T, dims) - -Read a series of values of type `T` from `stream`, in canonical binary representation. -`dims` is either a tuple or a series of integer arguments specifying the size of the `Array{T}` -to return. -""" -read(s::IO, ::Type{T}, dims::Dims) where {T} = read!(s, Array{T}(dims)) - @noinline function read!(s::IO, a::Array{UInt8}) # mark noinline to ensure the array is gc-rooted somewhere (by the caller) unsafe_read(s, pointer(a), sizeof(a)) return a @@ -523,7 +510,7 @@ end Read at most `nb` bytes from `s`, returning a `Vector{UInt8}` of the bytes read. """ -function read(s::IO, nb=typemax(Int)) +function read(s::IO, nb::Integer = typemax(Int)) # Let readbytes! grow the array progressively by default # instead of taking of risk of over-allocating b = Vector{UInt8}(nb == typemax(Int) ? 1024 : nb) diff --git a/base/serialize.jl b/base/serialize.jl index b8e2387e98d002..36b34967da144d 100644 --- a/base/serialize.jl +++ b/base/serialize.jl @@ -862,7 +862,7 @@ function deserialize_array(s::AbstractSerializer) end end else - A = read(s.io, elty, dims) + A = read!(s.io, Array{elty}(dims)) end s.table[slot] = A return A