Skip to content

Commit

Permalink
Resolving review topics around Encode function.
Browse files Browse the repository at this point in the history
Leaving the error return in, per
#136 (comment)

May also be interesting to note: I did check if ValidCode (or, roughly
the same thing but perhaps renamed to KnownCode) could be reintroduced,
and the answer is... actually no, not without broaching other issues.
A body of `_, ok := registry[code]` is enough to open a can of worms.
See
#136 (comment)
  • Loading branch information
warpfork committed Mar 9, 2021
1 parent 442f64a commit 67c208a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ func Decode(buf []byte) (*DecodedMultihash, error) {

// Encode a hash digest along with the specified function code.
// Note: the length is derived from the length of the digest itself.
//
// The error return is legacy; it is always nil.
func Encode(buf []byte, code uint64) ([]byte, error) {
// REVIEW: if we remove the strict ValidCode check, this can no longer error. Change signiture?
// REVIEW: this function always causes heap allocs... but when used, this value is almost always going to be appended to another buffer (either as part of CID creation, or etc) -- should this whole function be rethought and alternatives offered?

// FUTURE: this function always causes heap allocs... but when used, this value is almost always going to be appended to another buffer (either as part of CID creation, or etc) -- should this whole function be rethought and alternatives offered?
newBuf := make([]byte, varint.UvarintSize(code)+varint.UvarintSize(uint64(len(buf)))+len(buf))
n := varint.PutUvarint(newBuf, code)
n += varint.PutUvarint(newBuf[n:], uint64(len(buf)))
Expand Down

0 comments on commit 67c208a

Please sign in to comment.