-
Notifications
You must be signed in to change notification settings - Fork 0
/
protobuf-go-lite.go
42 lines (37 loc) · 1.02 KB
/
protobuf-go-lite.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
package protobuf_go_lite
// Message is the base vtprotobuf message marshal/unmarshal interface.
type Message interface {
MarshalVT() ([]byte, error)
UnmarshalVT([]byte) error
Reset()
}
// EqualVT is a message with a EqualVT function (VTProtobuf).
type EqualVT[T comparable] interface {
comparable
// EqualVT compares against the other message for equality.
EqualVT(other T) bool
}
// CompareEqualVT returns a compare function to compare two VTProtobuf messages.
func CompareEqualVT[T EqualVT[T]]() func(t1, t2 T) bool {
return func(t1, t2 T) bool {
return IsEqualVT(t1, t2)
}
}
// CompareComparable returns a compare function to compare two comparable types.
func CompareComparable[T comparable]() func(t1, t2 T) bool {
return func(t1, t2 T) bool {
return t1 == t2
}
}
// IsEqualVT checks if two EqualVT objects are equal.
func IsEqualVT[T EqualVT[T]](t1, t2 T) bool {
var empty T
t1Empty, t2Empty := t1 == empty, t2 == empty
if t1Empty != t2Empty {
return false
}
if t1Empty {
return true
}
return t1.EqualVT(t2)
}