bytes.go 515 B

123456789101112131415161718192021222324252627282930
  1. package bencode
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. type Bytes []byte
  7. var (
  8. _ Unmarshaler = (*Bytes)(nil)
  9. _ Marshaler = (*Bytes)(nil)
  10. _ Marshaler = Bytes{}
  11. )
  12. func (me *Bytes) UnmarshalBencode(b []byte) error {
  13. *me = append([]byte(nil), b...)
  14. return nil
  15. }
  16. func (me Bytes) MarshalBencode() ([]byte, error) {
  17. if len(me) == 0 {
  18. return nil, errors.New("marshalled Bytes should not be zero-length")
  19. }
  20. return me, nil
  21. }
  22. func (me Bytes) GoString() string {
  23. return fmt.Sprintf("bencode.Bytes(%q)", []byte(me))
  24. }