streamdata: optionally reuse buffer
This enables use of stdlib iterator helpers. Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
@@ -221,7 +221,7 @@ func main() {
|
||||
return errors.New("list requires a channel selected")
|
||||
}
|
||||
|
||||
for ident := range channel.All(&err) {
|
||||
for ident := range channel.All(&err, true) {
|
||||
fmt.Println(ident)
|
||||
}
|
||||
return
|
||||
|
||||
@@ -276,7 +276,10 @@ func (c *Channel) Path(ident *Ident) string {
|
||||
// All returns an iterator over all known [Ident] in the on-disk representation.
|
||||
// Iteration stops when encountering the first non-nil error, and its value is
|
||||
// saved to the value pointed to by errP.
|
||||
func (c *Channel) All(errP *error) iter.Seq[*Ident] {
|
||||
//
|
||||
// If reuse is true, the same value is updated every iteration and the same
|
||||
// address is yileded as a result.
|
||||
func (c *Channel) All(errP *error, reuse bool) iter.Seq[*Ident] {
|
||||
return func(yield func(*Ident) bool) {
|
||||
dents, err := c.root.FS().(fs.ReadDirFS).ReadDir(channelPathVOD)
|
||||
if err != nil {
|
||||
@@ -298,7 +301,11 @@ func (c *Channel) All(errP *error) iter.Seq[*Ident] {
|
||||
return
|
||||
}
|
||||
|
||||
if !yield(&ident) {
|
||||
p := &ident
|
||||
if !reuse {
|
||||
p = new(ident)
|
||||
}
|
||||
if !yield(p) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,7 +303,7 @@ func TestChannel(t *testing.T) {
|
||||
}
|
||||
|
||||
var iterErr error
|
||||
idents := slices.Collect(c.All(&iterErr))
|
||||
idents := slices.Collect(c.All(&iterErr, false))
|
||||
if iterErr != nil {
|
||||
t.Fatalf("All: error = %#v", iterErr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user