Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1089,10 +1089,9 @@ end

@inline peek(s::Stateful, sentinel=nothing) = s.nextvalstate !== nothing ? s.nextvalstate[1] : sentinel
@inline iterate(s::Stateful, state=nothing) = s.nextvalstate === nothing ? nothing : (popfirst!(s), nothing)
IteratorSize(::Type{Stateful{VS,T}} where VS) where {T} =
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite remember what the intent here was, but it may have been to filter out HasSize, since we don't pass that through (and can't really support).

isa(IteratorSize(T), SizeUnknown) ? SizeUnknown() : HasLength()
IteratorSize(::Type{Stateful{T,VS}}) where {T,VS} = IteratorSize(T) isa HasShape ? HasLength() : IteratorSize(T)
eltype(::Type{Stateful{T, VS}} where VS) where {T} = eltype(T)
IteratorEltype(::Type{Stateful{VS,T}} where VS) where {T} = IteratorEltype(T)
IteratorEltype(::Type{Stateful{T,VS}}) where {T,VS} = IteratorEltype(T)
length(s::Stateful) = length(s.itr) - s.taken

end
23 changes: 23 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,26 @@ end
@test @inferred(first(z)) == (1, "a", 1.0, 1, "a", 1.0, 1, "a", 1.0)
@test @inferred(first(Iterators.drop(z, 1))) == (2, "b", 2.0, 2, "a", 1.2, 1, "c", 1.0)
end

@testset "Stateful fix #30643" begin
@test Base.IteratorSize(1:10) isa Base.HasShape
a = Iterators.Stateful(1:10)
@test Base.IteratorSize(a) isa Base.HasLength
@test length(a) == 10
@test length(collect(a)) == 10
@test length(a) == 0
b = Iterators.Stateful(Iterators.take(1:10,3))
@test Base.IteratorSize(b) isa Base.HasLength
@test length(b) == 3
@test length(collect(b)) == 3
@test length(b) == 0
c = Iterators.Stateful(Iterators.countfrom(1))
@test Base.IteratorSize(c) isa Base.IsInfinite
@test length(Iterators.take(c,3)) == 3
@test length(collect(Iterators.take(c,3))) == 3
d = Iterators.Stateful(Iterators.filter(isodd,1:10))
@test Base.IteratorSize(d) isa Base.SizeUnknown
@test length(collect(Iterators.take(d,3))) == 3
@test length(collect(d)) == 2
@test length(collect(d)) == 0
end