upload.go 652 B

123456789101112131415161718192021222324252627
  1. package upload
  2. import (
  3. "iter"
  4. "golang.org/x/exp/constraints"
  5. )
  6. type Chunk[I constraints.Integer] struct {
  7. Offset I
  8. Size I
  9. }
  10. // Chunks yields a sequence of a part number and a Chunk. The Chunk is the offset
  11. // and size of the chunk. The last chunk may be smaller than chunkSize if size is
  12. // not a multiple of chunkSize.
  13. //
  14. // The first part number is 1 and increases monotonically.
  15. func Chunks[I constraints.Integer](size, chunkSize I) iter.Seq2[int, Chunk[I]] {
  16. return func(yield func(int, Chunk[I]) bool) {
  17. var n int
  18. for off := I(0); off < size; off += chunkSize {
  19. n++
  20. yield(n, Chunk[I]{off, min(chunkSize, size-off)})
  21. }
  22. }
  23. }