-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype_slice.go
58 lines (45 loc) · 933 Bytes
/
type_slice.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package parco
import (
"io"
)
type (
Iterable[T any] interface {
Len() int
Range(ranger[T]) error
Unwrap() SliceView[T]
}
SliceType[T any] struct {
length int
header IntType
inner Type[T]
}
)
func (t SliceType[T]) ByteLength() int {
return t.header.ByteLength() + t.length*t.inner.ByteLength()
}
func (t SliceType[T]) Parse(r io.Reader) (res Iterable[T], err error) {
var (
length int
)
length, err = t.header.Parse(r)
if err != nil {
return nil, err
}
arrType := Array[T](length, t.inner)
t.length = length
return arrType.Parse(r)
}
func (t SliceType[T]) Compile(x Iterable[T], w io.Writer) error {
t.length = x.Len()
if err := t.header.Compile(t.length, w); err != nil {
return err
}
arrType := Array[T](t.length, t.inner)
return arrType.Compile(x, w)
}
func Slice[T any](header IntType, inner Type[T]) SliceType[T] {
return SliceType[T]{
header: header,
inner: inner,
}
}