@@ -109,7 +109,8 @@ public def nil : Hash :=
109109
110110public instance : NilTrace Hash := ⟨nil⟩
111111
112- @[inline] public def ofNat (n : Nat) :=
112+ /-- Constructs a hash with with the value `n`. This is not equivalent to the hash of `n`. -/
113+ @[inline] public def ofNat (n : Nat) : Hash :=
113114 mk n.toUInt64
114115
115116/-- Parse a hash from a JSON number. -/
@@ -142,12 +143,14 @@ public def ofHex? (s : String) : Option Hash :=
142143public def hex (self : Hash) : String :=
143144 lpad (String.ofList <| Nat.toDigits 16 self.val.toNat) '0' 16
144145
146+ /-- Parse a hash from a string of decimal digits. -/
145147public def ofDecimal? (s : String) : Option Hash :=
146148 s.toNat?.map ofNat
147149
148150@[inline] public def ofString? (s : String) : Option Hash :=
149151 ofHex? s
150152
153+ /-- Laod a hash from a `.hash` file. -/
151154public def load? (hashFile : FilePath) : BaseIO (Option Hash) :=
152155 ofString? <$> IO.FS.readFile hashFile |>.catchExceptions fun _ => pure none
153156
@@ -161,18 +164,25 @@ public instance : MixTrace Hash := ⟨mix⟩
161164
162165public instance : ToString Hash := ⟨Hash.toString⟩
163166
164- @[inline] public def ofString (str : String) :=
165- mix nil <| mk <| hash str -- same as Name.mkSimple
167+ /-- Hash of a value using its `Hashable` instance. -/
168+ @[inline] public def ofHashable [Hashable α] (a : α) : Hash :=
169+ mix nil <| mk <| hash a -- same as Name.mkSimple
170+
171+ /-- Hash of a string (without line-ending normalization). -/
172+ @[inline] public def ofString (str : String) : Hash :=
173+ ofHashable str
166174
167175/-- Hash of a line-ending normalized string. -/
168- @[inline] public def ofText (str : String) :=
176+ @[inline] public def ofText (str : String) : Hash : =
169177 ofString str.crlfToLf
170178
179+ /-- Hash of a sequence of bytes. -/
171180@[inline] public def ofByteArray (bytes : ByteArray) : Hash :=
172- ⟨hash bytes⟩
181+ ofHashable bytes
173182
174- @[inline] public def ofBool (b : Bool) :=
175- mk (hash b)
183+ @ [inline, deprecated ofHashable (since := "2026-02-06" )]
184+ public def ofBool (b : Bool) : Hash :=
185+ ofHashable b
176186
177187@[inline] public protected def toJson (self : Hash) : Json :=
178188 toJson self.hex
@@ -208,8 +218,7 @@ public instance [ComputeHash α m] : ComputeTrace α m Hash := ⟨ComputeHash.co
208218@[inline] public def computeHash [ComputeHash α m] [MonadLiftT m n] (a : α) : n Hash :=
209219 liftM <| ComputeHash.computeHash a
210220
211- public instance : ComputeHash Bool Id := ⟨Hash.ofBool⟩
212- public instance : ComputeHash String Id := ⟨Hash.ofString⟩
221+ public instance [Hashable α] : ComputeHash α Id := ⟨Hash.ofHashable⟩
213222
214223/--
215224Compute the hash of a binary file.
0 commit comments