Skip to content

Commit

Permalink
Replace Deref impl on RsaPrivateKey with AsRef
Browse files Browse the repository at this point in the history
The `RsaPrivateKey` type previously had a `Deref` impl providing access
to the associated `RsaPublicKey`.

`Deref` is intended for "smart pointer types", i.e. container types
which manage a (typically generic) inner type in some way. This doesn't
seem like one of those cases.

`AsRef`, on the other hand, is for cheap reference conversions, which is
exactly what's happening here, so it's a better fit and provides the
same functionality (albeit explicitly rather than via deref coercion).
  • Loading branch information
tarcieri committed Apr 27, 2023
1 parent db2559f commit 05b1846
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ impl PartialEq for RsaPrivateKey {
}
}

impl AsRef<RsaPublicKey> for RsaPrivateKey {
fn as_ref(&self) -> &RsaPublicKey {
&self.pubkey_components
}
}

impl Hash for RsaPrivateKey {
fn hash<H: Hasher>(&self, state: &mut H) {
// Domain separator for RSA private keys
Expand All @@ -73,13 +79,6 @@ impl Drop for RsaPrivateKey {
}
}

impl Deref for RsaPrivateKey {
type Target = RsaPublicKey;
fn deref(&self) -> &RsaPublicKey {
&self.pubkey_components
}
}

impl ZeroizeOnDrop for RsaPrivateKey {}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 05b1846

Please sign in to comment.