indexid.go 829 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "encoding/binary"
  9. "errors"
  10. "fmt"
  11. "github.com/syncthing/syncthing/lib/rand"
  12. )
  13. type IndexID uint64
  14. func (i IndexID) String() string {
  15. return fmt.Sprintf("0x%016X", uint64(i))
  16. }
  17. func (i IndexID) Marshal() ([]byte, error) {
  18. bs := make([]byte, 8)
  19. binary.BigEndian.PutUint64(bs, uint64(i))
  20. return bs, nil
  21. }
  22. func (i *IndexID) Unmarshal(bs []byte) error {
  23. if len(bs) != 8 {
  24. return errors.New("incorrect IndexID length")
  25. }
  26. *i = IndexID(binary.BigEndian.Uint64(bs))
  27. return nil
  28. }
  29. func NewIndexID() IndexID {
  30. return IndexID(rand.Uint64())
  31. }