From 24970d5fc6cd9aa3458819e1e9970414fdf3702d Mon Sep 17 00:00:00 2001 From: Nathan Zimmerberg <39104088+nhz2@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:38:00 -0400 Subject: [PATCH] BREAKING: Make close a no-op (#3) * make close a no-op * fix julia 1.6 --- Project.toml | 2 +- src/InputBuffers.jl | 32 +++++++------------------------- test/runtests.jl | 34 ++++------------------------------ 3 files changed, 12 insertions(+), 56 deletions(-) diff --git a/Project.toml b/Project.toml index e078f7f..06f3a09 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "InputBuffers" uuid = "0c81fc1b-5583-44fc-8770-48be1e1cca08" authors = ["nhz2 and contributors"] -version = "0.1.1" +version = "0.2.0" [compat] julia = "1.6" diff --git a/src/InputBuffers.jl b/src/InputBuffers.jl index 34387ea..f732e8c 100644 --- a/src/InputBuffers.jl +++ b/src/InputBuffers.jl @@ -7,7 +7,6 @@ mutable struct InputBuffer{T<:AbstractVector{UInt8}} <: IO pos::Int64 size::Int64 mark::Int64 - opened::Bool end """ @@ -16,37 +15,23 @@ end Create a readable and seekable I/O stream wrapper around a vector of bytes. """ function InputBuffer(data::AbstractVector{UInt8}) - InputBuffer{typeof(data)}(data, 0, length(data), -1, true) -end - -function Base.close(b::InputBuffer)::Nothing - b.pos = 0 - b.mark = -1 - b.opened=false - nothing -end - -Base.isopen(b::InputBuffer)::Bool = b.opened -Base.isreadable(b::InputBuffer)::Bool = b.opened -Base.iswritable(b::InputBuffer)::Bool = false - -function _throw_closed_error() - throw(ArgumentError("read failed, InputBuffer is closed")) + InputBuffer{typeof(data)}(data, 0, length(data), -1) end +Base.close(::InputBuffer)::Nothing = nothing +Base.isopen(::InputBuffer)::Bool = true +Base.isreadable(::InputBuffer)::Bool = true +Base.iswritable(::InputBuffer)::Bool = false function Base.eof(b::InputBuffer)::Bool - isopen(b) || _throw_closed_error() b.pos === b.size end function Base.position(b::InputBuffer)::Int64 - isopen(b) || _throw_closed_error() b.pos end function Base.bytesavailable(b::InputBuffer)::Int64 - isopen(b) || _throw_closed_error() b.size-b.pos end @@ -54,13 +39,11 @@ end # --------------- function Base.seek(b::InputBuffer, n::Integer)::InputBuffer - isopen(b) || _throw_closed_error() b.pos = clamp(n, 0, b.size) b end function Base.seekend(b::InputBuffer)::InputBuffer - isopen(b) || _throw_closed_error() b.pos = b.size b end @@ -76,12 +59,11 @@ end # needed for `peek(b, Char)` to work function Base.peek(b::InputBuffer, ::Type{UInt8})::UInt8 - eof(b) && throw(EOFError()) # also prevents overflow and errors if closed + eof(b) && throw(EOFError()) b.data[firstindex(b.data) + b.pos] end function Base.skip(b::InputBuffer, n::Integer)::InputBuffer - isopen(b) || _throw_closed_error() b.pos += clamp(n, -b.pos, b.size - b.pos) b end @@ -114,7 +96,7 @@ const ByteVector = Union{ } function Base.unsafe_read(b::InputBuffer{<:ByteVector}, p::Ptr{UInt8}, n::UInt)::Nothing - nb::Int64 = min(n, bytesavailable(b)) # errors if closed + nb::Int64 = min(n, bytesavailable(b)) data = b.data GC.@preserve data unsafe_copyto!(p, pointer(data, Int(firstindex(data) + b.pos)), nb) b.pos += nb diff --git a/test/runtests.jl b/test/runtests.jl index d202e23..fad03f8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -190,8 +190,8 @@ using CRC32: crc32 @test !ismarked(stream) @test !unmark(stream) @test mark(stream) == 3 - close(stream) - @test !ismarked(stream) + close(stream) # This is a no-op + @test ismarked(stream) stream = InputBuffer(b"foobarbaz") @test stream == seek(stream, 2) @@ -215,11 +215,6 @@ using CRC32: crc32 @test eof(stream) close(stream) - stream = InputBuffer(b"") - @test eof(stream) - close(stream) - @test_throws ArgumentError eof(stream) # close - @testset "readuntil" begin stream = InputBuffer(b"") data = readuntil(stream, 0x00) @@ -277,33 +272,12 @@ using CRC32: crc32 @test data == zeros(UInt8, 5) end end - @testset "closing" begin + @testset "closing is no-op" begin b = InputBuffer(b"foo") - mark(b) - @test !iswritable(b) - @test isopen(b) - @test isreadable(b) - @test bytesavailable(b) == 3 - read(b) - @test !iswritable(b) + @test isnothing(close(b)) @test isopen(b) @test isreadable(b) - @test eof(b) - @test bytesavailable(b) == 0 - @test isnothing(close(b)) - @test !isopen(b) - @test !isreadable(b) @test !iswritable(b) - @test_throws ArgumentError eof(b) - @test !ismarked(b) - @test_throws ArgumentError mark(b) - @test_throws ArgumentError position(b) - @test_throws ArgumentError bytesavailable(b) - @test_throws ArgumentError read(b) - @test_throws ArgumentError read(b, String) - @test_throws ArgumentError read(b, UInt8) - @test_throws ArgumentError readbytes!(b, UInt8[]) - @test isnothing(close(b)) # second close should be noop end @testset "crc32" begin for trial in 1:100