Proposal to add method `Base.read(io::IO, ::Type{T}) where T <: IO`

,

Currently Base.read() does support

read(src, String)
read(src, Int32)

but the following signatures are failing

read(src, IOBuffer)
read(src, Base.SecretBuffer)

What do you think about introducing

"""
    read(io::IO, T::Type{<:IO}) -> T

Read the contents of `io` into a new instance of `T` and return it
rewound to the start. `T` must support a zero-argument constructor
and be seekable.

# Examples
# io  = IOBuffer("Hello World")
# buf = read(io, IOBuffer)
# sb  = read(open("/run/secrets/token"), SecretBuffer)
"""
function Base.read(io::IO, ::Type{T}) where T <: IO
    dst = T()
    write(dst, io)
    return seekstart(dst)
end

or shorter

Base.read(io::IO, ::Type{T}) where T <: IO =
    (dst = T(); write(dst, io); seekstart(dst))

I am aware that the signature is quite general and that the method will fail if a buffer doesn’t support the constructor call or isn’t seekable. But on the other hand the meaning of this signature is quite straight forward and probably meaningless for buffers that don’t fulfill that condition.
Would be happy to receive feedback.

the docstring says

  read(io::IO, T)

  Read a single value of type T from io

so I’d be expecting read(io::IO, ::Type{IO}) to read a single IO object off of IO. the functionality you propose I’d probably expect to be spelled more like read!(dst::IO, src::IO) ?

Your proposed functionality ist already covered by write(::IO, ::IO), I think.

I’m not sure about that actually

julia> io = IOBuffer(); io2 = IOBuffer();

julia> write(io, "abcde")
5

julia> write(io, io2)
0

julia> write(io2, io)
0

You need to rewind the buffer with seekstart(io)