revm_interpreter/instructions/
utility.rs

1use primitives::{Address, B256, U256};
2
3/// Trait for converting types into U256 values.
4pub trait IntoU256 {
5    /// Converts the implementing type into a U256 value.
6    fn into_u256(self) -> U256;
7}
8
9impl IntoU256 for Address {
10    fn into_u256(self) -> U256 {
11        self.into_word().into_u256()
12    }
13}
14
15impl IntoU256 for B256 {
16    fn into_u256(self) -> U256 {
17        U256::from_be_bytes(self.0)
18    }
19}
20
21/// Trait for converting types into Address values.
22pub trait IntoAddress {
23    /// Converts the implementing type into an Address value.
24    fn into_address(self) -> Address;
25}
26
27impl IntoAddress for U256 {
28    fn into_address(self) -> Address {
29        Address::from_word(B256::from(self.to_be_bytes()))
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use primitives::address;
36
37    use super::*;
38
39    #[test]
40    fn test_into_u256() {
41        let addr = address!("0x0000000000000000000000000000000000000001");
42        let u256 = addr.into_u256();
43        assert_eq!(u256, U256::from(0x01));
44        assert_eq!(u256.into_address(), addr);
45    }
46}