Clarify and fix State's EOF behavior

This commit is contained in:
2024-09-30 15:33:24 -06:00
parent c29be1a7b6
commit 526e40323d
3 changed files with 88 additions and 22 deletions

View File

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"math"
"slices"
"strings"
)
@ -142,10 +143,19 @@ type State[In any] struct {
}
func (s State[In]) Read(dst []In) (n uint64, next State[In], err error) {
if s.pos > math.MaxInt64 {
return 0, s, io.EOF
}
nread, err := s.r.ReadAt(dst, int64(s.pos))
if nread > 0 {
s.pos += uint64(nread)
}
if nread == len(dst) && err == io.EOF {
if nread == 0 {
return 0, s, io.EOF
}
return uint64(nread), s, nil
}
return uint64(nread), s, err
}