Skip to content

Commit

Permalink
Use iter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Jul 9, 2024
1 parent bd5be4c commit 057f8af
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
2 changes: 2 additions & 0 deletions ext/fileio/coro.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !go1.23

package fileio

import (
Expand Down
17 changes: 3 additions & 14 deletions ext/fileio/fsdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (d fsdir) Open() (sqlite3.VTabCursor, error) {
type cursor struct {
fsdir
base string
resume func(struct{}) (entry, bool)
resume resume
cancel func()
curr entry
eof bool
Expand Down Expand Up @@ -92,25 +92,14 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
c.base = base
}

c.resume, c.cancel = coroNew(func(_ struct{}, yield func(entry) struct{}) entry {
walkDir := func(path string, d fs.DirEntry, err error) error {
yield(entry{d, err, path})
return nil
}
if c.fsys != nil {
fs.WalkDir(c.fsys, root, walkDir)
} else {
filepath.WalkDir(root, walkDir)
}
return entry{}
})
c.resume, c.cancel = pull(c, root)
c.eof = false
c.rowID = 0
return c.Next()
}

func (c *cursor) Next() error {
curr, ok := c.resume(struct{}{})
curr, ok := next(c)
c.curr = curr
c.eof = !ok
c.rowID++
Expand Down
29 changes: 29 additions & 0 deletions ext/fileio/fsdir_coro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !go1.23

package fileio

import (
"io/fs"
"path/filepath"
)

type resume = func(struct{}) (entry, bool)

func next(c *cursor) (entry, bool) {
return c.resume(struct{}{})
}

func pull(c *cursor, root string) (resume, func()) {
return coroNew(func(_ struct{}, yield func(entry) struct{}) entry {
walkDir := func(path string, d fs.DirEntry, err error) error {
yield(entry{d, err, path})
return nil
}
if c.fsys != nil {
fs.WalkDir(c.fsys, root, walkDir)
} else {
filepath.WalkDir(root, walkDir)
}
return entry{}
})
}
31 changes: 31 additions & 0 deletions ext/fileio/fsdir_iter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build go1.23

package fileio

import (
"io/fs"
"iter"
"path/filepath"
)

type resume = func() (entry, bool)

func next(c *cursor) (entry, bool) {
return c.resume()
}

func pull(c *cursor, root string) (resume, func()) {
return iter.Pull(func(yield func(entry) bool) {
walkDir := func(path string, d fs.DirEntry, err error) error {
if yield(entry{d, err, path}) {
return nil
}
return fs.SkipAll
}
if c.fsys != nil {
fs.WalkDir(c.fsys, root, walkDir)
} else {
filepath.WalkDir(root, walkDir)
}
})
}

0 comments on commit 057f8af

Please sign in to comment.