Unverified Commit c1c89c20 authored by Whyrusleeping's avatar Whyrusleeping Committed by GitHub

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

add a couple useful methods
parents 723b4ab1 628a0123
......@@ -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) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment