xrpl_common_stdlib/types/
account_id.rs1use crate::fields::decoder::{FieldDecoder, FromCurrentTx, FromLedger, decode_exact};
7use crate::types::decode_error::DecodeError;
8
9pub const ACCOUNT_ID_SIZE: usize = 20;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23#[repr(C)]
24pub struct AccountID(pub [u8; ACCOUNT_ID_SIZE]);
25
26impl From<[u8; ACCOUNT_ID_SIZE]> for AccountID {
27 fn from(value: [u8; ACCOUNT_ID_SIZE]) -> Self {
28 AccountID(value)
29 }
30}
31
32impl FieldDecoder for AccountID {
35 type Buffer = [u8; ACCOUNT_ID_SIZE];
36
37 #[inline]
38 fn empty_buffer() -> Self::Buffer {
39 [0u8; ACCOUNT_ID_SIZE]
40 }
41
42 #[inline]
43 fn decode(buf: Self::Buffer, bytes_written: usize) -> core::result::Result<Self, DecodeError> {
44 decode_exact(buf, bytes_written)
45 }
46}
47
48impl FromCurrentTx for AccountID {}
49impl FromLedger for AccountID {}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_account_id_byte_order_preserved() {
57 let mut bytes = [0u8; ACCOUNT_ID_SIZE];
59 for (i, byte) in bytes.iter_mut().enumerate() {
60 *byte = i as u8;
61 }
62
63 let account_id = AccountID::from(bytes);
64
65 for i in 0..ACCOUNT_ID_SIZE {
67 assert_eq!(account_id.0[i], i as u8);
68 }
69 }
70}