Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add useful helper function tee? #47200

Open
vtjnash opened this issue Oct 17, 2022 · 1 comment
Open

add useful helper function tee? #47200

vtjnash opened this issue Oct 17, 2022 · 1 comment
Labels
io Involving the I/O subsystem: libuv, read, write, etc.

Comments

@vtjnash
Copy link
Member

vtjnash commented Oct 17, 2022

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))
@IanButterworth
Copy link
Member

Related #34363

@JeffBezanson JeffBezanson added the io Involving the I/O subsystem: libuv, read, write, etc. label Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
io Involving the I/O subsystem: libuv, read, write, etc.
Projects
None yet
Development

No branches or pull requests

3 participants