From 0dedb795faceef48ce8aa5385808d4ab91d02c30 Mon Sep 17 00:00:00 2001 From: Jakob Nybo Nissen Date: Wed, 15 Sep 2021 17:17:12 +0200 Subject: [PATCH] Add open for composed transcodingstreams --- src/stream.jl | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/stream.jl b/src/stream.jl index ba596c09..1da489a9 100644 --- a/src/stream.jl +++ b/src/stream.jl @@ -168,6 +168,48 @@ function Base.open(f::Function, ::Type{T}, args...) where T<:TranscodingStream end end +# ComposedFunction is available in Julia 1.6 +@static if VERSION ≥ v"1.6" + function Base.open( + f::Function, + code::ComposedFunction{ + <:Union{<:Type{<:TranscodingStream}, <:ComposedFunction}, + <:Type{<:TranscodingStream} + }, + path::AbstractString + ) + io = open(code, path) + try + f(io) + finally + close(io) + end + end + + # Allow open with chained TranscodingStreams + function Base.open( + code::ComposedFunction{ + <:Union{<:Type{<:TranscodingStream}, <:ComposedFunction}, + <:Type{<:TranscodingStream} + }, + path::AbstractString + ) + _open(typeof(code), open(path)) + end + + function _open(code::Type{T}, io::IO) where { + S <: TranscodingStream, + U <: Union{<:Type{<:TranscodingStream}, <:ComposedFunction}, + T <: ComposedFunction{U,<:Type{S}} + } + _open(U, S(io)) + end + + function _open(code::Type{<:Type{T}}, io::IO) where {T <: TranscodingStream} + T(io) + end +end + function Base.isopen(stream::TranscodingStream) return stream.state.mode != :close && stream.state.mode != :panic end