github.com/koko1123/flow-go-1@v0.29.6/model/encoding/rlp/codec.go (about) 1 package rlp 2 3 import ( 4 "io" 5 6 "github.com/ethereum/go-ethereum/rlp" 7 8 "github.com/koko1123/flow-go-1/model/encoding" 9 ) 10 11 var _ encoding.Marshaler = (*Marshaler)(nil) 12 13 type Marshaler struct{} 14 15 func NewMarshaler() *Marshaler { 16 return &Marshaler{} 17 } 18 19 func (m *Marshaler) Marshal(val interface{}) ([]byte, error) { 20 return rlp.EncodeToBytes(val) 21 } 22 23 func (m *Marshaler) Unmarshal(b []byte, val interface{}) error { 24 return rlp.DecodeBytes(b, val) 25 } 26 27 func (m *Marshaler) MustMarshal(val interface{}) []byte { 28 b, err := m.Marshal(val) 29 if err != nil { 30 panic(err) 31 } 32 33 return b 34 } 35 36 func (m *Marshaler) MustUnmarshal(b []byte, val interface{}) { 37 err := m.Unmarshal(b, val) 38 if err != nil { 39 panic(err) 40 } 41 } 42 43 var _ encoding.Codec = (*Codec)(nil) 44 45 type Codec struct{} 46 47 func (c *Codec) NewEncoder(w io.Writer) encoding.Encoder { 48 return &Encoder{w} 49 } 50 51 func (c *Codec) NewDecoder(r io.Reader) encoding.Decoder { 52 return &Decoder{r} 53 } 54 55 type Encoder struct { 56 w io.Writer 57 } 58 59 func (e *Encoder) Encode(v interface{}) error { 60 return rlp.Encode(e.w, v) 61 } 62 63 type Decoder struct { 64 r io.Reader 65 } 66 67 func (e *Decoder) Decode(v interface{}) error { 68 return rlp.Decode(e.r, v) 69 }