vector_xdr.go 822 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (C) 2015 The Protocol Authors.
  2. package protocol
  3. import "github.com/calmh/xdr"
  4. // This stuff is hacked up manually because genxdr doesn't support 'type
  5. // Vector []Counter' declarations and it was tricky when I tried to add it...
  6. func (v Vector) MarshalXDRInto(m *xdr.Marshaller) error {
  7. m.MarshalUint32(uint32(len(v)))
  8. for i := range v {
  9. m.MarshalUint64(uint64(v[i].ID))
  10. m.MarshalUint64(v[i].Value)
  11. }
  12. return m.Error
  13. }
  14. func (v *Vector) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
  15. l := int(u.UnmarshalUint32())
  16. if l > 1e6 {
  17. return xdr.ElementSizeExceeded("number of counters", l, 1e6)
  18. }
  19. n := make(Vector, l)
  20. for i := range n {
  21. n[i].ID = ShortID(u.UnmarshalUint64())
  22. n[i].Value = u.UnmarshalUint64()
  23. }
  24. *v = n
  25. return u.Error
  26. }
  27. func (v Vector) XDRSize() int {
  28. return 4 + 16*len(v)
  29. }