pathfinder2/src/types/address.rs
2022-09-13 16:54:15 +02:00

39 lines
928 B
Rust

use std::fmt::{Display, Formatter, Debug};
#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Address([u8; 20]);
impl Debug for Address {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl From<[u8; 20]> for Address {
fn from(item: [u8; 20]) -> Self {
Address(item)
}
}
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)
}
}
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(())
}
}