Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add utils to ValueOrArray #924

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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