pathfinder2/src/types/address.rs

21 lines
554 B
Rust
Raw Normal View History

2022-08-30 22:42:53 +00:00
#[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq, PartialOrd)]
2022-08-30 16:53:34 +00:00
pub struct Address([u8; 20]);
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)
}
}