Skip to content

Commit

Permalink
Remove internal checkmode macro (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 authored Mar 25, 2024
1 parent 3bf110a commit 710dff1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
12 changes: 6 additions & 6 deletions src/noop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ Note that this method may return a wrong position when
"""
function Base.position(stream::NoopStream)
mode = stream.state.mode
@checkmode (:idle, :read, :write)
if mode === :idle
return Int64(0)
elseif mode === :write
return position(stream.stream) + buffersize(stream.buffer1)
elseif mode === :read
return position(stream.stream) - buffersize(stream.buffer1)
else
throw_invalid_mode(mode)
end
@assert false "unreachable"
end
Expand Down Expand Up @@ -146,19 +147,18 @@ end
function stats(stream::NoopStream)
state = stream.state
mode = state.mode
@checkmode (:idle, :read, :write)
buffer = stream.buffer1
@assert buffer === stream.buffer2
if mode == :idle
if mode === :idle
consumed = supplied = 0
elseif mode == :read
elseif mode === :read
supplied = buffer.transcoded
consumed = supplied - buffersize(buffer)
elseif mode == :write
elseif mode === :write
supplied = buffer.transcoded + buffersize(buffer)
consumed = buffer.transcoded
else
@assert false "unreachable"
throw_invalid_mode(mode)
end
return Stats(consumed, supplied, supplied, supplied)
end
Expand Down
34 changes: 15 additions & 19 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,8 @@ function splitkwargs(kwargs, keys)
end
@specialize

# Check that mode is valid.
macro checkmode(validmodes)
mode = esc(:mode)
validmodes = Any[arg for arg in validmodes.args]
quote
if !$(foldr((x, y) -> :($(mode) === $x || $(y)), validmodes, init=false))
throw(ArgumentError(string("invalid mode :", $(mode))))
end
end
end
# throw ArgumentError that mode is invalid.
throw_invalid_mode(mode) = throw(ArgumentError(string("invalid mode :", mode)))


# Base IO Functions
Expand Down Expand Up @@ -291,13 +283,14 @@ codec may change the length of data.
"""
function Base.position(stream::TranscodingStream)
mode = stream.state.mode
@checkmode (:idle, :read, :write)
if mode === :idle
return Int64(0)
elseif mode === :read
return stats(stream).out
elseif mode === :write
return stats(stream).in
else
throw_invalid_mode(mode)
end
@assert false "unreachable"
end
Expand All @@ -308,23 +301,27 @@ end

function Base.seekstart(stream::TranscodingStream)
mode = stream.state.mode
@checkmode (:idle, :read)
if mode == :read
if mode === :read
callstartproc(stream, mode)
emptybuffer!(stream.buffer1)
emptybuffer!(stream.buffer2)
elseif mode === :idle
else
throw_invalid_mode(mode)
end
seekstart(stream.stream)
return stream
end

function Base.seekend(stream::TranscodingStream)
mode = stream.state.mode
@checkmode (:idle, :read)
if mode == :read
callstartproc(stream, mode)
emptybuffer!(stream.buffer1)
emptybuffer!(stream.buffer2)
elseif mode === :idle
else
throw_invalid_mode(mode)
end
seekend(stream.stream)
return stream
Expand Down Expand Up @@ -578,23 +575,22 @@ Create an I/O statistics object of `stream`.
function stats(stream::TranscodingStream)
state = stream.state
mode = state.mode
@checkmode (:idle, :read, :write)
buffer1 = stream.buffer1
buffer2 = stream.buffer2
if mode == :idle
if mode === :idle
transcoded_in = transcoded_out = in = out = 0
elseif mode == :read
elseif mode === :read
transcoded_in = buffer2.transcoded
transcoded_out = buffer1.transcoded
in = transcoded_in + buffersize(buffer2)
out = transcoded_out - buffersize(buffer1)
elseif mode == :write
elseif mode === :write
transcoded_in = buffer1.transcoded
transcoded_out = buffer2.transcoded
in = transcoded_in + buffersize(buffer1)
out = transcoded_out - buffersize(buffer2)
else
@assert false "unreachable"
throw_invalid_mode(mode)
end
return Stats(in, out, transcoded_in, transcoded_out)
end
Expand Down
2 changes: 2 additions & 0 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@
end
@test position(stream) == position(sink) == position(iob)
close(stream)
@test_throws ArgumentError position(stream)
@test_throws ArgumentError TranscodingStreams.stats(stream)
close(iob)

mktemp() do path, sink
Expand Down
2 changes: 2 additions & 0 deletions test/codecquadruple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ end
@test position(stream) == position(iob)
end
close(stream)
@test_throws ArgumentError position(stream)
@test_throws ArgumentError TranscodingStreams.stats(stream)
close(iob)

mktemp() do path, sink
Expand Down

0 comments on commit 710dff1

Please sign in to comment.