set_data_from_slice (#15854)
* data_ensure_equal_to_slice * rename and change to copy
This commit is contained in:
committed by
GitHub
parent
654449ce91
commit
806bfdd67b
@ -394,6 +394,20 @@ impl Account {
|
||||
}
|
||||
|
||||
impl AccountSharedData {
|
||||
/// make account's data equal to 'data'. This may require resizing and copying data.
|
||||
pub fn set_data_from_slice(&mut self, data: &[u8]) {
|
||||
let len = self.data.len();
|
||||
let len_different = len != data.len();
|
||||
if len_different {
|
||||
// if the resize causes a reallocation and copy, it would be better to create a new copy of the final data
|
||||
// rather than resize (+ copy current) and then copy over below.
|
||||
// however, the implementation of account's data is soon to be copy on write, so the tradeoffs will soon be different.
|
||||
self.data.resize(data.len(), 0);
|
||||
}
|
||||
// we could compare here to determine whether we need to modify the original data or not. In the current implementation, that would
|
||||
// not make a positive difference.
|
||||
self.data.copy_from_slice(data);
|
||||
}
|
||||
pub fn set_data(&mut self, data: Vec<u8>) {
|
||||
self.data = data;
|
||||
}
|
||||
@ -533,6 +547,27 @@ pub mod tests {
|
||||
(account1, account2)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_account_set_data_from_slice() {
|
||||
let key = Pubkey::new_unique();
|
||||
let (_, mut account) = make_two_accounts(&key);
|
||||
assert_eq!(account.data(), &vec![0, 0]);
|
||||
account.set_data_from_slice(&[1, 2]);
|
||||
assert_eq!(account.data(), &vec![1, 2]);
|
||||
account.set_data_from_slice(&[1, 2, 3]);
|
||||
assert_eq!(account.data(), &vec![1, 2, 3]);
|
||||
account.set_data_from_slice(&[4, 5, 6]);
|
||||
assert_eq!(account.data(), &vec![4, 5, 6]);
|
||||
account.set_data_from_slice(&[4, 5, 6, 0]);
|
||||
assert_eq!(account.data(), &vec![4, 5, 6, 0]);
|
||||
account.set_data_from_slice(&[]);
|
||||
assert_eq!(account.data().len(), 0);
|
||||
account.set_data_from_slice(&[44]);
|
||||
assert_eq!(account.data(), &vec![44]);
|
||||
account.set_data_from_slice(&[44]);
|
||||
assert_eq!(account.data(), &vec![44]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_account_data_set_data() {
|
||||
let key = Pubkey::new_unique();
|
||||
|
Reference in New Issue
Block a user