Merge pull request #102 from ipfs/feat/add-methods-for-using-less-memory

add a couple useful methods
This commit is contained in:
Whyrusleeping
2020-05-01 15:54:35 -07:00
committed by GitHub

18
cid.go
View File

@@ -25,6 +25,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
mbase "github.com/multiformats/go-multibase"
@@ -408,6 +409,23 @@ func (c Cid) Bytes() []byte {
return []byte(c.str)
}
// ByteLen returns the length of the CID in bytes.
// It's equivalent to `len(c.Bytes())`, but works without an allocation,
// and should therefore be preferred.
//
// (See also the WriteTo method for other important operations that work without allocation.)
func (c Cid) ByteLen() int {
return len(c.str)
}
// WriteTo writes the CID bytes to the given writer.
// This method works without incurring any allocation.
//
// (See also the ByteLen method for other important operations that work without allocation.)
func (c Cid) WriteTo(w io.Writer) (int, error) {
return io.WriteString(w, c.str)
}
// MarshalBinary is equivalent to Bytes(). It implements the
// encoding.BinaryMarshaler interface.
func (c Cid) MarshalBinary() ([]byte, error) {