github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/libbox/iterator.go (about) 1 package libbox 2 3 import "github.com/sagernet/sing/common" 4 5 type StringIterator interface { 6 Next() string 7 HasNext() bool 8 } 9 10 var _ StringIterator = (*iterator[string])(nil) 11 12 type iterator[T any] struct { 13 values []T 14 } 15 16 func newIterator[T any](values []T) *iterator[T] { 17 return &iterator[T]{values} 18 } 19 20 func (i *iterator[T]) Next() T { 21 if len(i.values) == 0 { 22 return common.DefaultValue[T]() 23 } 24 nextValue := i.values[0] 25 i.values = i.values[1:] 26 return nextValue 27 } 28 29 func (i *iterator[T]) HasNext() bool { 30 return len(i.values) > 0 31 } 32 33 type abstractIterator[T any] interface { 34 Next() T 35 HasNext() bool 36 } 37 38 func iteratorToArray[T any](iterator abstractIterator[T]) []T { 39 if iterator == nil { 40 return nil 41 } 42 var values []T 43 for iterator.HasNext() { 44 values = append(values, iterator.Next()) 45 } 46 return values 47 }