Skip to content

Commit 7c82f3b

Browse files
Merge pull request #103 from ipfs/fix/write-to-interface
avoid calling the method WriteTo if we don't satisfy its contract
2 parents c1c89c2 + 75caa6b commit 7c82f3b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cid.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,19 @@ func (c Cid) ByteLen() int {
418418
return len(c.str)
419419
}
420420

421-
// WriteTo writes the CID bytes to the given writer.
421+
// WriteBytes writes the CID bytes to the given writer.
422422
// This method works without incurring any allocation.
423423
//
424424
// (See also the ByteLen method for other important operations that work without allocation.)
425-
func (c Cid) WriteTo(w io.Writer) (int, error) {
426-
return io.WriteString(w, c.str)
425+
func (c Cid) WriteBytes(w io.Writer) (int, error) {
426+
n, err := io.WriteString(w, c.str)
427+
if err != nil {
428+
return n, err
429+
}
430+
if n != len(c.str) {
431+
return n, fmt.Errorf("failed to write entire cid string")
432+
}
433+
return n, nil
427434
}
428435

429436
// MarshalBinary is equivalent to Bytes(). It implements the

0 commit comments

Comments
 (0)