-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.go
More file actions
46 lines (40 loc) · 1.21 KB
/
pointer.go
File metadata and controls
46 lines (40 loc) · 1.21 KB
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
package lexy
// pointerCodec is the Codec for pointers, using elemCodec to encode and decode its referent.
// A pointer is encoded as:
// - if nil, prefixNilFirst/Last
// - if non-nil, prefixNonNil followed by its encoded referent
type pointerCodec[E any] struct {
elemCodec Codec[E]
prefix Prefix
}
func (c pointerCodec[E]) Append(buf []byte, value *E) []byte {
done, buf := c.prefix.Append(buf, value == nil)
if done {
return buf
}
return c.elemCodec.Append(buf, *value)
}
func (c pointerCodec[E]) Put(buf []byte, value *E) []byte {
done, buf := c.prefix.Put(buf, value == nil)
if done {
return buf
}
return c.elemCodec.Put(buf, *value)
}
func (c pointerCodec[E]) Get(buf []byte) (*E, []byte) {
done, buf := c.prefix.Get(buf)
if done {
return nil, buf
}
value, buf := c.elemCodec.Get(buf)
return &value, buf
}
func (c pointerCodec[E]) RequiresTerminator() bool {
// A encoded nil pointer cannot be a prefix of an encoded non-nil pointer,
// so this Codec requires escaping if and only if the element Codec does.
return c.elemCodec.RequiresTerminator()
}
//lint:ignore U1000 this is actually used
func (c pointerCodec[E]) nilsLast() Codec[*E] {
return pointerCodec[E]{c.elemCodec, PrefixNilsLast}
}