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 open for composed transcodingstreams #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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 @@ -177,6 +177,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