From 531e8012351b7fc8fb6054dd8635c0935cfc821f Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Mon, 22 Jul 2024 09:40:09 +0200 Subject: [PATCH] feat(json-rpc): implement `From for Id` and `From for Id` (#1088) feat(json-rpc): implement From for Id and From for Id --- crates/json-rpc/src/common.rs | 16 ++++++++++++++-- crates/rpc-client/src/client.rs | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/json-rpc/src/common.rs b/crates/json-rpc/src/common.rs index 5c23e0fae16..c47228446fd 100644 --- a/crates/json-rpc/src/common.rs +++ b/crates/json-rpc/src/common.rs @@ -32,6 +32,18 @@ pub enum Id { None, } +impl From for Id { + fn from(value: u64) -> Self { + Self::Number(value) + } +} + +impl From for Id { + fn from(value: String) -> Self { + Self::String(value) + } +} + impl Display for Id { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -70,14 +82,14 @@ impl<'de> Deserialize<'de> for Id { where E: serde::de::Error, { - Ok(Id::Number(v)) + Ok(v.into()) } fn visit_str(self, v: &str) -> Result where E: serde::de::Error, { - Ok(Id::String(v.to_owned())) + Ok(v.to_owned().into()) } fn visit_none(self) -> Result diff --git a/crates/rpc-client/src/client.rs b/crates/rpc-client/src/client.rs index 75ed70c99cb..e52ca93278a 100644 --- a/crates/rpc-client/src/client.rs +++ b/crates/rpc-client/src/client.rs @@ -255,7 +255,7 @@ impl RpcClientInner { /// Reserve a request ID u64. #[inline] pub fn next_id(&self) -> Id { - Id::Number(self.increment_id()) + self.increment_id().into() } }