streamdata: optionally reuse buffer

This enables use of stdlib iterator helpers.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
2026-03-20 03:17:37 +09:00
parent ebb49770e7
commit 3389848f45
3 changed files with 11 additions and 4 deletions

View File

@@ -221,7 +221,7 @@ func main() {
return errors.New("list requires a channel selected") return errors.New("list requires a channel selected")
} }
for ident := range channel.All(&err) { for ident := range channel.All(&err, true) {
fmt.Println(ident) fmt.Println(ident)
} }
return return

View File

@@ -276,7 +276,10 @@ func (c *Channel) Path(ident *Ident) string {
// All returns an iterator over all known [Ident] in the on-disk representation. // 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 // Iteration stops when encountering the first non-nil error, and its value is
// saved to the value pointed to by errP. // 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) { return func(yield func(*Ident) bool) {
dents, err := c.root.FS().(fs.ReadDirFS).ReadDir(channelPathVOD) dents, err := c.root.FS().(fs.ReadDirFS).ReadDir(channelPathVOD)
if err != nil { if err != nil {
@@ -298,7 +301,11 @@ func (c *Channel) All(errP *error) iter.Seq[*Ident] {
return return
} }
if !yield(&ident) { p := &ident
if !reuse {
p = new(ident)
}
if !yield(p) {
return return
} }
} }

View File

@@ -303,7 +303,7 @@ func TestChannel(t *testing.T) {
} }
var iterErr error var iterErr error
idents := slices.Collect(c.All(&iterErr)) idents := slices.Collect(c.All(&iterErr, false))
if iterErr != nil { if iterErr != nil {
t.Fatalf("All: error = %#v", iterErr) t.Fatalf("All: error = %#v", iterErr)
} }