forked from Minetest-j45/mt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzerialize.go
56 lines (44 loc) · 948 Bytes
/
zerialize.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
// This file is called zerialize.go so the following go:generate runs last.
//go:generate ./mkserialize.sh
package mt
import (
"encoding/binary"
"errors"
"io"
)
// ErrTooLong reports a length that is too long to serialize.
var ErrTooLong = errors.New("len too long")
var be = binary.BigEndian
type serializer interface {
Serialize(w io.Writer)
}
func serialize(w io.Writer, s interface{}) error {
return pcall(func() { s.(serializer).Serialize(w) })
}
type deserializer interface {
Deserialize(r io.Reader)
}
func deserialize(r io.Reader, d interface{}) error {
return pcall(func() { d.(deserializer).Deserialize(r) })
}
type serializationError struct {
error
}
func pcall(f func()) (rerr error) {
defer func() {
switch r := recover().(type) {
case serializationError:
rerr = r.error
case nil:
default:
panic(r)
}
}()
f()
return
}
func chk(err error) {
if err != nil {
panic(serializationError{err})
}
}