tape: Add back iter compatibility for table decoding

This commit is contained in:
Sasha Koshka 2025-05-18 16:12:55 -04:00
parent 568431f4c3
commit 3a88619f9b

View File

@ -1,5 +1,7 @@
package tape
import "iter"
// encoding and decoding functions must not make any allocations
type TablePushFunc func(tag uint16, value []byte) (n int, err error)
@ -22,6 +24,17 @@ func DecodeTable(data []byte) TablePullFunc {
}
}
func DecodeTableIter(data []byte) iter.Seq2[uint16, []byte] {
return func(yield func(uint16, []byte) bool) {
pull := DecodeTable(data)
for {
tag, value, _, err := pull()
if err != nil { return }
if !yield(tag, value) { return }
}
}
}
func EncodeTable(data []byte) TablePushFunc {
return func(tag uint16, value []byte) (n int, err error) {
if n >= len(data) { return n, ErrWrongBufferLength }