solana/sdk/src/feature.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

//! Methods for working with `Feature` accounts.
2021-03-10 13:28:03 -08:00
use crate::account::{AccountSharedData, ReadableAccount, WritableAccount};
2020-10-30 13:40:55 -07:00
pub use solana_program::feature::*;
2020-09-21 14:03:35 -07:00
2021-03-09 13:06:07 -08:00
pub fn from_account<T: ReadableAccount>(account: &T) -> Option<Feature> {
if account.owner() != &id() {
2020-10-30 13:40:55 -07:00
None
} else {
2021-03-09 13:06:07 -08:00
bincode::deserialize(account.data()).ok()
2020-10-30 13:40:55 -07:00
}
}
2020-09-21 14:03:35 -07:00
2021-03-09 13:06:07 -08:00
pub fn to_account(feature: &Feature, account: &mut AccountSharedData) -> Option<()> {
2021-03-10 13:28:03 -08:00
bincode::serialize_into(account.data_as_mut_slice(), feature).ok()
2020-09-21 14:03:35 -07:00
}
2021-03-09 13:06:07 -08:00
pub fn create_account(feature: &Feature, lamports: u64) -> AccountSharedData {
2020-10-30 13:40:55 -07:00
let data_len = Feature::size_of().max(bincode::serialized_size(feature).unwrap() as usize);
2021-03-09 13:06:07 -08:00
let mut account = AccountSharedData::new(lamports, data_len, &id());
2020-10-30 13:40:55 -07:00
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() {
2021-03-09 13:06:07 -08:00
let just_initialized = AccountSharedData::new(42, Feature::size_of(), &id());
2020-09-28 09:02:14 -07:00
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 })
);
}
}