1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::pubkey::Pubkey;
use std::{cmp, fmt};
pub struct AccountInfo<'a> {
pub key: &'a Pubkey,
pub is_signer: bool,
pub lamports: &'a mut u64,
pub data: &'a mut [u8],
pub owner: &'a Pubkey,
}
impl<'a> fmt::Debug for AccountInfo<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let data_len = cmp::min(64, self.data.len());
let data_str = if data_len > 0 {
format!(" data: {}", hex::encode(self.data[..data_len].to_vec()))
} else {
"".to_string()
};
write!(
f,
"AccountInfo {{ lamports: {} data.len: {} owner: {} {} }}",
self.lamports,
self.data.len(),
self.owner,
data_str,
)
}
}
impl<'a> AccountInfo<'a> {
pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> {
bincode::deserialize(&self.data)
}
pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> {
if bincode::serialized_size(state)? > self.data.len() as u64 {
return Err(Box::new(bincode::ErrorKind::SizeLimit));
}
bincode::serialize_into(&mut self.data[..], state)
}
}