Open
Description
Would this be a helpful function to include in Base? I wrote it for some debugging work (and contrib/generate_precompile.jl has a copy of this already for debugging)
# returns a new stream that has the identical content to `in`, but also "tees"
# the `transform(readavailable(in)::Vector{UInt8})` first to `out`
function tee(f, in::IO)
copy = Base.BufferStream()
t = @async try
while !eof(in)
l = readavailable(in)
f(l)
write(copy, l)
end
catch ex
if !(ex isa Base.IOError && ex.code == Base.UV_EIO)
rethrow() # ignore EIO on `in` stream
end
finally
# TODO: could we call closewrite to propagate an error, instead of always doing a clean close here?
closewrite(copy)
end
Base.errormonitor(t)
return copy
end
tee(out::IO, in::IO) = tee(l -> write(out, l), in)
# tee((in, out)::Pair) = (in::IO; out::IO; tee(l -> write(out, l), in))
Activity