Skip to content

Commit

Permalink
feat: add utils to ValueOrArray (alloy-rs#924)
Browse files Browse the repository at this point in the history
* feat: add utils to ValueOrArray

* fix clippy
  • Loading branch information
tcoratger authored and ben186 committed Jul 27, 2024
1 parent 1dd8ddc commit b69f18c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions crates/rpc-types-eth/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,36 @@ pub enum ValueOrArray<T> {
Array(Vec<T>),
}

impl<T> ValueOrArray<T> {
/// Get the value if present.
pub const 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 const fn is_value(&self) -> bool {
matches!(self, Self::Value(_))
}

/// Check if the enum is an array.
pub const fn is_array(&self) -> bool {
matches!(self, Self::Array(_))
}
}

impl From<Address> for ValueOrArray<Address> {
fn from(src: Address) -> Self {
Self::Value(src)
Expand Down

0 comments on commit b69f18c

Please sign in to comment.