-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuffer.go
More file actions
34 lines (28 loc) · 779 Bytes
/
Copy pathbuffer.go
File metadata and controls
34 lines (28 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package nasync
type buffer struct {
buf []*task // contents are the bytes buf[off : len(buf)]
off int // read at &buf[off], write at &buf[len(buf)]
}
func (b *buffer) Reset() {
b.Truncate(0)
}
func (b *buffer) Len() int { return len(b.buf) - b.off }
// Truncate discards all but the first n unread bytes from the buffer
// but continues to use the same allocated storage.
// It panics if n is negative or greater than the length of the buffer.
func (b *buffer) Truncate(n int) {
switch {
case n < 0 || n > b.Len():
panic("Buffer: truncation out of range")
case n == 0:
// Reuse buffer space.
b.off = 0
}
b.buf = b.buf[0 : b.off+n]
}
func (b *buffer) Tasks() []*task {
return b.buf[b.off:]
}
func (b *buffer) Append(t *task) {
b.buf = append(b.buf, t)
}