mod.rs
Overview
This file defines and implements the AccountAddress struct, a wrapper around a 256-bit unsigned integer (UInt256), representing an account address within the system. It provides essential conversions, formatting, serialization, and bitwise operations for the address type. The file also exposes a submodule direct_bit_access_operations for related low-level bit manipulation functionality.
AccountAddress Struct
Description
AccountAddress encapsulates a UInt256 value, representing a 256-bit account address. This abstraction allows for convenient handling, display, and interoperability with other types such as tvm_types::AccountId.
Derivations
ClonePartialEq
These traits enable copying, comparison, hashing, and ordering of AccountAddress instances.
Fields
pub 0: UInt256— the underlying 256-bit integer.
Methods
to_hex_string(&self) -> String
Returns a hexadecimal string representation of the account address.
Example:
let addr = AccountAddress(UInt256::default());
println!("{}", addr.to_hex_string());
This method calls to_hex_string on the inner UInt256.
Trait Implementations
Display
Formats the AccountAddress as a hexadecimal string for user-facing output.
Uses
to_hex_stringinternally.Implements the
fmtmethod of thestd::fmt::Displaytrait.
Debug
Provides debug formatting by outputting the hex string representation of the address.
Implements
fmtofstd::fmt::Debug.
FromStr
Enables parsing an AccountAddress from a string.
Input:
&strOutput:
Result<AccountAddress, tvm_types::Error>Internally parses the string as a
UInt256.
Default
Creates a default AccountAddress initialized with a zeroed UInt256.
Serialize and Deserialize
Supports serialization and deserialization via serde:
Serializes the address as a 32-byte array.
Deserializes from a 32-byte array into
UInt256, then wraps it.
This allows integration with external formats such as JSON or binary.
From Conversions
Conversions between AccountAddress and tvm_types::AccountId:
From
AccountAddress(and its reference) toAccountIdvia innerUInt256.From
AccountId(and its reference) toAccountAddressby extracting the byte string and creating aUInt256from it.
These conversions facilitate interoperability with other system components that use AccountId types.
BitAnd Operator Overload for References
Implements the bitwise AND operator (&) for references to AccountAddress.
Performs bitwise AND on each byte of the underlying 32-byte arrays.
Returns a new
AccountAddresswith the result.
Note: The implementation could be optimized for performance.
Example:
let a: AccountAddress = ...;
let b: AccountAddress = ...;
let result = &a & &b;
Submodule: direct_bit_access_operations
This submodule is publicly exposed but its contents are not detailed here. It likely contains additional bit manipulation utilities for AccountAddress or related types.
Implementation Details and Algorithms
The bitwise AND operation is implemented byte-wise, iterating over the 32-byte arrays.
Serialization uses fixed-size arrays to ensure consistent binary representation.
Conversion from
AccountIdusesget_bytestring(0)to extract the underlying byte representation.Formatting leverages the
to_hex_string()method ofUInt256to present addresses as hex strings.
Interaction with Other System Components
The
AccountAddressstruct bridges between raw 256-bit unsigned integers (UInt256) andtvm_types::AccountIdtypes used elsewhere.Serialization compatibility enables communication or storage in formats expected by other modules.
The exposed submodule
direct_bit_access_operationssuggests integration with lower-level operations on addresses.Conversion traits ensure seamless use of
AccountAddresswhen interfacing with APIs or libraries expectingAccountId.
Diagram: AccountAddress Structure and Key Traits
classDiagram
class AccountAddress {
+UInt256 0
+to_hex_string()
+bitand()
}
AccountAddress ..> UInt256 : encapsulates
AccountAddress ..> tvm_types::AccountId : converts to/from
AccountAddress ..|> std::fmt::Display
AccountAddress ..|> std::fmt::Debug
AccountAddress ..|> std::str::FromStr
AccountAddress ..|> serde::Serialize
AccountAddress ..|> serde::Deserialize
AccountAddress ..|> std::default::Default
AccountAddress ..|> std::ops::BitAnd
This diagram illustrates relationships of AccountAddress with underlying data and implemented traits.