]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/bytes.go
bencode: Fix marshalling of unaddressable array of bytes
[btrtrc.git] / bencode / bytes.go
1 package bencode
2
3 import (
4         "errors"
5 )
6
7 type Bytes []byte
8
9 var (
10         _ Unmarshaler = (*Bytes)(nil)
11         _ Marshaler   = (*Bytes)(nil)
12         _ Marshaler   = Bytes{}
13 )
14
15 func (me *Bytes) UnmarshalBencode(b []byte) error {
16         *me = append([]byte(nil), b...)
17         return nil
18 }
19
20 func (me Bytes) MarshalBencode() ([]byte, error) {
21         if len(me) == 0 {
22                 return nil, errors.New("marshalled Bytes should not be zero-length")
23         }
24         return me, nil
25 }