pathfinder2/src/types/address.rs

39 lines
928 B
Rust
Raw Normal View History

2022-09-13 14:54:15 +00:00
use std::fmt::{Display, Formatter, Debug};
2022-09-01 21:04:36 +00:00
2022-09-13 14:54:15 +00:00
#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
2022-08-30 16:53:34 +00:00
pub struct Address([u8; 20]);
2022-09-13 14:54:15 +00:00
impl Debug for Address {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
2022-08-30 16:53:34 +00:00
impl From<[u8; 20]> for Address {
fn from(item: [u8; 20]) -> Self {
Address(item)
}
}
2022-08-31 17:20:39 +00:00
impl From<&str> for Address {
fn from(item: &str) -> Self {
assert!(item.starts_with("0x"));
assert!(item.len() == 2 + 20 * 2);
let mut data = [0u8; 20];
for i in (2..item.len()).step_by(2) {
data[i / 2 - 1] = u8::from_str_radix(&item[i..i + 2], 16).unwrap();
}
Address(data)
}
}
2022-09-01 21:04:36 +00:00
impl Display for Address {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "0x")?;
for b in self.0 {
write!(f, "{:02x}", b)?;
}
Ok(())
}
}