Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +217 to +219
Copy link
Member

@mkitti mkitti Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment about what this does?

end

function Base.isopen(stream::TranscodingStream)
return stream.state.mode != :close && stream.state.mode != :panic
end
Expand Down