From afdf1f21da083b8d14aed282c48c6bd5c2b99e07 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Mon, 24 Oct 2022 16:02:05 +0100 Subject: [PATCH] Prefer implementing `Display` to `From` for `String` (#7914) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There’s still `impl From for String`. It’s left intentionally as it avoids string allocation when used compared to using Display. --- chain/client-primitives/src/types.rs | 16 ---------------- core/crypto/src/signature.rs | 27 ++++++++------------------- 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/chain/client-primitives/src/types.rs b/chain/client-primitives/src/types.rs index 369046ae86a..785262a87bd 100644 --- a/chain/client-primitives/src/types.rs +++ b/chain/client-primitives/src/types.rs @@ -600,22 +600,6 @@ pub enum TxStatusError { TimeoutError, } -impl From for String { - fn from(error: TxStatusError) -> Self { - match error { - TxStatusError::ChainError(err) => format!("Chain error: {}", err), - TxStatusError::MissingTransaction(tx_hash) => { - format!("Transaction {} doesn't exist", tx_hash) - } - TxStatusError::InternalError(debug_message) => { - format!("Internal error: {}", debug_message) - } - TxStatusError::TimeoutError => format!("Timeout error"), - TxStatusError::InvalidTx(e) => format!("Invalid transaction: {}", e), - } - } -} - impl Message for TxStatus { type Result = Result, TxStatusError>; } diff --git a/core/crypto/src/signature.rs b/core/crypto/src/signature.rs index 3a31a6211f1..9b123085330 100644 --- a/core/crypto/src/signature.rs +++ b/core/crypto/src/signature.rs @@ -255,14 +255,18 @@ impl Hash for PublicKey { } impl Display for PublicKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - write!(f, "{}", String::from(self)) + fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result { + let (key_type, key_data) = match self { + PublicKey::ED25519(public_key) => (KeyType::ED25519, &public_key.0[..]), + PublicKey::SECP256K1(public_key) => (KeyType::SECP256K1, &public_key.0[..]), + }; + write!(fmt, "{}:{}", key_type, bs58::encode(key_data).into_string()) } } impl Debug for PublicKey { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - write!(f, "{}", String::from(self)) + Display::fmt(self, f) } } @@ -305,7 +309,7 @@ impl serde::Serialize for PublicKey { where S: serde::Serializer, { - serializer.serialize_str(&String::from(self)) + serializer.collect_str(self) } } @@ -320,21 +324,6 @@ impl<'de> serde::Deserialize<'de> for PublicKey { } } -impl From<&PublicKey> for String { - fn from(public_key: &PublicKey) -> Self { - match public_key { - PublicKey::ED25519(public_key) => { - format!("{}:{}", KeyType::ED25519, bs58::encode(&public_key.0).into_string()) - } - PublicKey::SECP256K1(public_key) => format!( - "{}:{}", - KeyType::SECP256K1, - bs58::encode(&public_key.0.to_vec()).into_string() - ), - } - } -} - impl FromStr for PublicKey { type Err = crate::errors::ParseKeyError;