Skip to content

Commit 9998d3f

Browse files
author
Robert Wright
committed
Tightened CircularBuffer push! arg types to avoid bad state bug
1 parent 99d214b commit 9998d3f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/circular_buffer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ end
101101
102102
Add an element to the back and overwrite front if full.
103103
"""
104-
@inline function Base.push!(cb::CircularBuffer, data)
104+
@inline function Base.push!(cb::CircularBuffer{T}, data::S) where {T, S <: T}
105105
# if full, increment and overwrite, otherwise push
106106
if cb.length == cb.capacity
107107
cb.first = (cb.first == cb.capacity ? 1 : cb.first + 1)

test/test_circular_buffer.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,12 @@
164164
@test Array(cb) == [21, 42, 42]
165165
end
166166
end
167+
168+
@testset "Issue 754" begin
169+
cb = CircularBuffer{Int}(5)
170+
@test size(cb) == (0,)
171+
@test_throws MethodError push!(cb, 1.5)
172+
@test size(cb) == (0,)
173+
end
174+
167175
end

0 commit comments

Comments
 (0)