Skip to content

Commit

Permalink
Widen signature of read!(::IO, ::Array) to AbstractArray (#33046)
Browse files Browse the repository at this point in the history
Co-Authored-By: Matt Bauman <mbauman@gmail.com>
  • Loading branch information
2 people authored and StefanKarpinski committed Dec 10, 2019
1 parent 46b7bb2 commit 1dbccbd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 4 additions & 4 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ read(filename::AbstractString, args...) = open(io->read(io, args...), filename)
read(filename::AbstractString, ::Type{T}) where {T} = open(io->read(io, T), filename)

"""
read!(stream::IO, array::Union{Array, BitArray})
read!(filename::AbstractString, array::Union{Array, BitArray})
read!(stream::IO, array::AbstractArray)
read!(filename::AbstractString, array::AbstractArray)
Read binary data from an I/O stream or file, filling in `array`.
"""
Expand Down Expand Up @@ -682,8 +682,8 @@ function read!(s::IO, a::Array{UInt8})
return a
end

function read!(s::IO, a::Array{T}) where T
if isbitstype(T)
function read!(s::IO, a::AbstractArray{T}) where T
if isbitstype(T) && (a isa Array || a isa FastContiguousSubArray{T,<:Any,<:Array{T}})
GC.@preserve a unsafe_read(s, pointer(a), sizeof(a))
else
for i in eachindex(a)
Expand Down
22 changes: 22 additions & 0 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,25 @@ end

# issue #26419
@test Base.return_types(read, (String, Type{String})) == Any[String]

@testset "read! to view" begin
x = rand(4, 4)
y = rand(10)
z = 1:10
v = [1.0, 2.0, 3.0, 4.0]
io = IOBuffer()
write(io, v)
flush(io)
seekstart(io)
read!(io, @view x[:, 3])
@test x[:, 3] == v
x = rand(3, 3)
seekstart(io)
read!(io, @view x[1:2, 2:3])
@test x[1:2, 2:3][:] == v[:]
seekstart(io)
read!(io, @view y[4:7])
@test y[4:7] == v
seekstart(io)
@test_throws ErrorException read!(io, @view z[4:6])
end

0 comments on commit 1dbccbd

Please sign in to comment.