-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmus.go
56 lines (44 loc) · 1.55 KB
/
mus.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
package mus
// Marshaller is the interface that wraps the Marshal method.
//
// Marshal method returns the number of used bytes. It should panic if receives
// too small bs.
type Marshaller[T any] interface {
Marshal(t T, bs []byte) (n int)
}
// MarshallerFn is a functional implementation of the Marshaller interface.
type MarshallerFn[T any] func(t T, bs []byte) (n int)
func (fn MarshallerFn[T]) Marshal(t T, bs []byte) (n int) {
return fn(t, bs)
}
// Unmarshaller is the interface that wraps the Unmarshal method.
//
// Unmarshal method returns data, the number of used bytes and an error.
type Unmarshaller[T any] interface {
Unmarshal(bs []byte) (t T, n int, err error)
}
// UnmarshallerFn is a functional implementation of the Unmarshaller interface.
type UnmarshallerFn[T any] func(bs []byte) (t T, n int, err error)
func (fn UnmarshallerFn[T]) Unmarshal(bs []byte) (t T, n int, err error) {
return fn(bs)
}
// Sizer is the interface that wraps the Size method.
type Sizer[T any] interface {
Size(t T) (size int)
}
// SizerFn is a functional implementation of the Sizer interface.
type SizerFn[T any] func(t T) (size int)
func (fn SizerFn[T]) Size(t T) (size int) {
return fn(t)
}
// Skipper is the interface that wraps the Skip method.
//
// Skip method returns the number of skipped bytes and an error.
type Skipper interface {
Skip(bs []byte) (n int, err error)
}
// SkipperFn is a functional implementation of the Skipper interface.
type SkipperFn func(bs []byte) (n int, err error)
func (fn SkipperFn) Skip(bs []byte) (n int, err error) {
return fn(bs)
}