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.