Skip to content

add useful helper function tee? #47200

Open
@vtjnash

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    ioInvolving the I/O subsystem: libuv, read, write, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions