Skip to content

Commit a928965

Browse files
author
Robert Wright
committed
push now mimics Base approach
1 parent 9998d3f commit a928965

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/circular_buffer.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,18 @@ end
101101
102102
Add an element to the back and overwrite front if full.
103103
"""
104-
@inline function Base.push!(cb::CircularBuffer{T}, data::S) where {T, S <: T}
104+
@inline function Base.push!(cb::CircularBuffer{T}, data) where T
105+
106+
# As per the behaviour of Base.push!
107+
data_converted = convert(T, data)
108+
105109
# if full, increment and overwrite, otherwise push
106110
if cb.length == cb.capacity
107111
cb.first = (cb.first == cb.capacity ? 1 : cb.first + 1)
108112
else
109113
cb.length += 1
110114
end
111-
@inbounds cb.buffer[_buffer_index(cb, cb.length)] = data
115+
@inbounds cb.buffer[_buffer_index(cb, cb.length)] = data_converted
112116
return cb
113117
end
114118

test/test_circular_buffer.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@
168168
@testset "Issue 754" begin
169169
cb = CircularBuffer{Int}(5)
170170
@test size(cb) == (0,)
171-
@test_throws MethodError push!(cb, 1.5)
171+
@test_throws InexactError push!(cb, 1.5)
172172
@test size(cb) == (0,)
173+
push!(cb, 1.0)
174+
@test size(cb) == (1,)
173175
end
174176

175177
end

0 commit comments

Comments
 (0)