Skip to content

Commit

Permalink
feat(json-rpc): implement From<u64> for Id and From<String> for Id (
Browse files Browse the repository at this point in the history
alloy-rs#1088)

feat(json-rpc): implement From<u64> for Id and From<String> for Id
  • Loading branch information
tcoratger authored and ben186 committed Jul 27, 2024
1 parent 80fe8ff commit 531e801
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions crates/json-rpc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ pub enum Id {
None,
}

impl From<u64> for Id {
fn from(value: u64) -> Self {
Self::Number(value)
}
}

impl From<String> 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 {
Expand Down Expand Up @@ -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<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Id::String(v.to_owned()))
Ok(v.to_owned().into())
}

fn visit_none<E>(self) -> Result<Self::Value, E>
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl<T> RpcClientInner<T> {
/// Reserve a request ID u64.
#[inline]
pub fn next_id(&self) -> Id {
Id::Number(self.increment_id())
self.increment_id().into()
}
}

Expand Down

0 comments on commit 531e801

Please sign in to comment.