Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a couple useful methods #102

Merged
merged 3 commits into from
May 1, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"

mbase "github.com/multiformats/go-multibase"
Expand Down Expand Up @@ -408,6 +409,17 @@ func (c Cid) Bytes() []byte {
return []byte(c.str)
}

// ByteLen returns len(c.Bytes()) without an allocation
func (c Cid) ByteLen() int {
return len(c.str)
}

// WriteTo writes the cids bytes to the given writer
func (c Cid) WriteTo(w io.Writer) error {
_, err := io.WriteString(w, c.str)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth checking if this has fully written the string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, probably better just to satisfy the WriterTo interface and return the write size

Copy link

@willscott willscott May 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't work with the IO methods unless it has a loop as i'm reading the interface contract.
It should either go until there's an error, or until it's written the full string - right?
ref: https://golang.org/pkg/io/#WriterTo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, i think youre right there.... Maybe i'll pick a different function name. I don't particularly care for loops here

return err
}

// MarshalBinary is equivalent to Bytes(). It implements the
// encoding.BinaryMarshaler interface.
func (c Cid) MarshalBinary() ([]byte, error) {
Expand Down