Skip to content

Commit

Permalink
Use deref methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Jan 6, 2025
1 parent 35b5a26 commit 81ccfc0
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions crates/rbuilder/src/blocklist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloy_primitives::Address;
use serde::{Deserialize, Deserializer};
use std::convert::TryFrom;
use std::fs::read_to_string;
use std::ops::Deref;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;

#[allow(clippy::len_without_is_empty)]
Expand All @@ -27,19 +27,6 @@ impl BlockList {
list: blocklist.into_iter().collect(),
})
}

pub fn len(&self) -> usize {
self.list.len()
}

pub fn contains(&self, address: &Address) -> bool {
self.list.contains(address)
}

#[cfg(test)]
fn add(&mut self, address: Address) {
self.list.insert(address);
}
}

impl TryFrom<PathBuf> for BlockList {
Expand All @@ -66,6 +53,12 @@ impl Deref for BlockList {
}
}

impl DerefMut for BlockList {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.list
}
}

impl<'de> Deserialize<'de> for BlockList {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -108,18 +101,18 @@ mod test {
let mut blocklist = BlockList::new();
let addr0 = Address::random();

blocklist.add(addr0);
blocklist.insert(addr0);
assert_eq!(blocklist.len(), 1);
assert_eq!(blocklist.contains(&addr0), true);

// you cannot add twice the same value
blocklist.add(addr0);
blocklist.insert(addr0);
assert_eq!(blocklist.len(), 1);

let addr1 = Address::random();
assert_eq!(blocklist.contains(&addr1), false);

blocklist.add(addr1);
blocklist.insert(addr1);
assert_eq!(blocklist.len(), 2);
assert_eq!(blocklist.contains(&addr1), true);
}
Expand Down

0 comments on commit 81ccfc0

Please sign in to comment.