Skip to content

Commit

Permalink
BREAKING: Make close a no-op (#3)
Browse files Browse the repository at this point in the history
* make close a no-op

* fix julia 1.6
  • Loading branch information
nhz2 authored Jul 12, 2024
1 parent 5f61dc6 commit 24970d5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "InputBuffers"
uuid = "0c81fc1b-5583-44fc-8770-48be1e1cca08"
authors = ["nhz2 <[email protected]> and contributors"]
version = "0.1.1"
version = "0.2.0"

[compat]
julia = "1.6"
32 changes: 7 additions & 25 deletions src/InputBuffers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mutable struct InputBuffer{T<:AbstractVector{UInt8}} <: IO
pos::Int64
size::Int64
mark::Int64
opened::Bool
end

"""
Expand All @@ -16,51 +15,35 @@ 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

# Seek Operations
# ---------------

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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 4 additions & 30 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

2 comments on commit 24970d5

@nhz2
Copy link
Member Author

@nhz2 nhz2 commented on 24970d5 Jul 12, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/110981

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 24970d5fc6cd9aa3458819e1e9970414fdf3702d
git push origin v0.2.0

Please sign in to comment.