Skip to content

Commit 6863922

Browse files
authored
mcf: add PasswordHash::as_password_hash_ref (#2117)
Adds an explicit method for borrowing a `&PasswordHashRef` from `&PasswordHash`, which is useful over `AsRef`/`Borrow`/`Deref` when avoiding inference and being explicit is preferable. To denote the pending breaking changes, this also bumps the version to v0.6.0-pre, so as to match `password-hash` and `phc`'s version.
1 parent 8c721b5 commit 6863922

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mcf/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mcf"
3-
version = "0.2.0"
3+
version = "0.6.0-pre"
44
authors = ["RustCrypto Developers"]
55
edition = "2024"
66
rust-version = "1.85"

mcf/src/lib.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ mod allocating {
147147
Ok(Self(s))
148148
}
149149

150+
/// Borrow the contents of this password hash as a [`PasswordHashRef`].
151+
///
152+
/// Similar conversions can be performed using [`AsRef`], [`Borrow`], and [`Deref`], however
153+
/// this one is useful when the return type may be ambiguous and avoiding potential
154+
/// inference bugs is preferable.
155+
pub fn as_password_hash_ref(&self) -> &PasswordHashRef {
156+
PasswordHashRef::new_unchecked(&self.0)
157+
}
158+
150159
/// Create an [`PasswordHash`] from an identifier.
151160
///
152161
/// # Returns
@@ -207,21 +216,21 @@ mod allocating {
207216

208217
impl AsRef<PasswordHashRef> for PasswordHash {
209218
fn as_ref(&self) -> &PasswordHashRef {
210-
PasswordHashRef::new_unchecked(&self.0)
219+
self.as_password_hash_ref()
211220
}
212221
}
213222

214223
impl Borrow<PasswordHashRef> for PasswordHash {
215224
fn borrow(&self) -> &PasswordHashRef {
216-
self.as_ref()
225+
self.as_password_hash_ref()
217226
}
218227
}
219228

220229
impl Deref for PasswordHash {
221230
type Target = PasswordHashRef;
222231

223232
fn deref(&self) -> &PasswordHashRef {
224-
self.as_ref()
233+
self.as_password_hash_ref()
225234
}
226235
}
227236

@@ -231,6 +240,12 @@ mod allocating {
231240
}
232241
}
233242

243+
impl From<&PasswordHash> for String {
244+
fn from(hash: &PasswordHash) -> Self {
245+
hash.0.clone()
246+
}
247+
}
248+
234249
impl FromStr for PasswordHash {
235250
type Err = Error;
236251

@@ -267,7 +282,7 @@ mod allocating {
267282

268283
impl<'a> From<&'a PasswordHash> for &'a PasswordHashRef {
269284
fn from(hash: &'a PasswordHash) -> &'a PasswordHashRef {
270-
hash.as_ref()
285+
hash.as_password_hash_ref()
271286
}
272287
}
273288

0 commit comments

Comments
 (0)