Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/vendor/github.com/anacrolix/missinggo/iter/iterable.go

70 lignes
1,1 Kio
Go
Brut Vue normale Historique

package iter
import (
"sync"
"github.com/anacrolix/missinggo"
)
type Iterable interface {
Iter(func(value interface{}) (more bool))
}
type iterator struct {
it Iterable
ch chan interface{}
value interface{}
ok bool
mu sync.Mutex
stopped missinggo.Event
}
func NewIterator(it Iterable) (ret *iterator) {
ret = &iterator{
it: it,
ch: make(chan interface{}),
}
go func() {
// Have to do this in a goroutine, because the interface is synchronous.
it.Iter(func(value interface{}) bool {
select {
case ret.ch <- value:
return true
case <-ret.stopped.LockedChan(&ret.mu):
return false
}
})
close(ret.ch)
ret.mu.Lock()
ret.stopped.Set()
ret.mu.Unlock()
}()
return
}
func (me *iterator) Value() interface{} {
if !me.ok {
panic("no value")
}
return me.value
}
func (me *iterator) Next() bool {
me.value, me.ok = <-me.ch
return me.ok
}
func (me *iterator) Stop() {
me.mu.Lock()
me.stopped.Set()
me.mu.Unlock()
}
func IterableAsSlice(it Iterable) (ret []interface{}) {
it.Iter(func(value interface{}) bool {
ret = append(ret, value)
return true
})
return
}