]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Implement encoding.TextMarshaler to match unmarshaler for metainfo.Hash
authorMatt Joiner <anacrolix@gmail.com>
Thu, 21 Jan 2021 22:49:51 +0000 (09:49 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 21 Jan 2021 22:53:53 +0000 (09:53 +1100)
Fixes https://github.com/anacrolix/torrent/issues/463

metainfo/hash.go

index b2bab751b6564927590562c3ab28a2fb3f5e37f7..0e258f3d16a17b3e30cf46275b7418c3f9332459 100644 (file)
@@ -12,10 +12,7 @@ const HashSize = 20
 // 20-byte SHA1 hash used for info and pieces.
 type Hash [HashSize]byte
 
-var (
-       _ fmt.Formatter            = (*Hash)(nil)
-       _ encoding.TextUnmarshaler = (*Hash)(nil)
-)
+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
@@ -54,9 +51,17 @@ func (h *Hash) FromHexString(s string) (err error) {
        return
 }
 
+var (
+       _ encoding.TextUnmarshaler = (*Hash)(nil)
+       _ encoding.TextMarshaler   = Hash{}
+)
+
 func (h *Hash) UnmarshalText(b []byte) error {
        return h.FromHexString(string(b))
 }
+func (h Hash) MarshalText() (text []byte, err error) {
+       return []byte(h.HexString()), nil
+}
 
 func NewHashFromHex(s string) (h Hash) {
        err := h.FromHexString(s)