From 32a39391ad6ca9ac9738deac9c2d1888ea436c59 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 17 Jun 2024 14:09:26 +0200 Subject: [PATCH 1/2] feat: add utils to ValueOrArray --- crates/rpc-types-eth/src/filter.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/rpc-types-eth/src/filter.rs b/crates/rpc-types-eth/src/filter.rs index d1d62775563..0bdca9e5994 100644 --- a/crates/rpc-types-eth/src/filter.rs +++ b/crates/rpc-types-eth/src/filter.rs @@ -668,6 +668,36 @@ pub enum ValueOrArray { Array(Vec), } +impl ValueOrArray { + /// Get the value if present. + pub fn as_value(&self) -> Option<&T> { + if let Self::Value(value) = self { + Some(value) + } else { + None + } + } + + /// Get the array if present. + pub fn as_array(&self) -> Option<&[T]> { + if let Self::Array(array) = self { + Some(array) + } else { + None + } + } + + /// Check if the enum is a single value. + pub fn is_value(&self) -> bool { + matches!(self, Self::Value(_)) + } + + /// Check if the enum is an array. + pub fn is_array(&self) -> bool { + matches!(self, Self::Array(_)) + } +} + impl From
for ValueOrArray
{ fn from(src: Address) -> Self { Self::Value(src) From 55ca0f47a1d7ab9ed85180b5106436fee26523d6 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 17 Jun 2024 14:12:09 +0200 Subject: [PATCH 2/2] fix clippy --- crates/rpc-types-eth/src/filter.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/rpc-types-eth/src/filter.rs b/crates/rpc-types-eth/src/filter.rs index 0bdca9e5994..63357bcf767 100644 --- a/crates/rpc-types-eth/src/filter.rs +++ b/crates/rpc-types-eth/src/filter.rs @@ -670,7 +670,7 @@ pub enum ValueOrArray { impl ValueOrArray { /// Get the value if present. - pub fn as_value(&self) -> Option<&T> { + pub const fn as_value(&self) -> Option<&T> { if let Self::Value(value) = self { Some(value) } else { @@ -688,12 +688,12 @@ impl ValueOrArray { } /// Check if the enum is a single value. - pub fn is_value(&self) -> bool { + pub const fn is_value(&self) -> bool { matches!(self, Self::Value(_)) } /// Check if the enum is an array. - pub fn is_array(&self) -> bool { + pub const fn is_array(&self) -> bool { matches!(self, Self::Array(_)) } }