solana/sdk/src/feature.rs

36 lines
976 B
Rust
Raw Normal View History

2020-10-30 13:40:55 -07:00
use crate::account::Account;
pub use solana_program::feature::*;
2020-09-21 14:03:35 -07:00
2020-10-30 13:40:55 -07:00
pub fn from_account(account: &Account) -> Option<Feature> {
if account.owner != id() {
None
} else {
bincode::deserialize(&account.data).ok()
}
}
2020-09-21 14:03:35 -07:00
2020-10-30 13:40:55 -07:00
pub fn to_account(feature: &Feature, account: &mut Account) -> Option<()> {
bincode::serialize_into(&mut account.data[..], feature).ok()
2020-09-21 14:03:35 -07:00
}
2020-10-30 13:40:55 -07:00
pub fn create_account(feature: &Feature, lamports: u64) -> Account {
let data_len = Feature::size_of().max(bincode::serialized_size(feature).unwrap() as usize);
let mut account = Account::new(lamports, data_len, &id());
to_account(feature, &mut account).unwrap();
account
2020-09-21 14:03:35 -07:00
}
2020-09-28 09:02:14 -07:00
#[cfg(test)]
mod test {
use super::*;
#[test]
fn feature_deserialize_none() {
let just_initialized = Account::new(42, Feature::size_of(), &id());
assert_eq!(
2020-10-30 13:40:55 -07:00
from_account(&just_initialized),
2020-09-28 09:02:14 -07:00
Some(Feature { activated_at: None })
);
}
}