Implement fmt.Formatter for metainfo.Hash

It's so easy to make mistakes by specifying %x when printing these.
This commit is contained in:
Matt Joiner 2020-11-11 15:31:55 +11:00
parent 6b5da26d38
commit 493ee9dfd1
1 changed files with 8 additions and 0 deletions

View File

@ -11,6 +11,14 @@ const HashSize = 20
// 20-byte SHA1 hash used for info and pieces. // 20-byte SHA1 hash used for info and pieces.
type Hash [HashSize]byte type Hash [HashSize]byte
var _ fmt.Formatter = (*Hash)(nil)
func (h Hash) Format(f fmt.State, c rune) {
// TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless
// with the "default" 'v', or .String() already returning the hex.
f.Write([]byte(h.HexString()))
}
func (h Hash) Bytes() []byte { func (h Hash) Bytes() []byte {
return h[:] return h[:]
} }