Skip to content

Commit cab106d

Browse files
committed
consuming account write
1 parent ae3ebac commit cab106d

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/putils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "putils"
3-
version = "0.0.12"
3+
version = "0.1.0"
44
description = "Utilities for writing Solana programs with the pinocchio framework"
55
keywords = ["solana", "pinocchio"]
66
documentation = "https://docs.rs/putils/latest/putils/"

crates/putils/src/account.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,23 @@ pub trait AccountDeserialize: AccountDiscriminator + Sized {
5858
}
5959

6060
/// The AccountWrite trait is used to handle persisting accounts into [`AccountInfo`]
61-
pub trait AccountWrite: AccountSerialize {
61+
///
62+
/// [`AccountWrite`] is consumes the implementing object, which is designed as a safety
63+
/// measure designed to prevent partial state change persistence.
64+
///
65+
/// For example if you have a lending pool, we dont want to write lending reserve state changes
66+
/// such as interest rate adjustments, accept deposits, make changes to the tracked deposited amounts
67+
/// but forget to persist those state changes.
68+
pub trait AccountWrite: AccountSerialize + Sized {
6269
/// Writes the serialized account (with discriminator)
63-
fn account_write(&self, account_info: &AccountInfo) -> ProgramResult {
70+
fn account_write(self, account_info: &AccountInfo) -> ProgramResult {
6471
let mut data = account_info.try_borrow_mut_data()?;
6572

6673
self.account_write_into(&mut data[..Self::SERIALIZED_SIZE])
6774
}
6875

6976
/// Writes the serialized account (with discriminator) into an arbitrary buffer
70-
fn account_write_into(&self, buffer: &mut [u8]) -> Result<(), ProgramError> {
77+
fn account_write_into(self, buffer: &mut [u8]) -> Result<(), ProgramError> {
7178
self.into_bytes(buffer)
7279
}
7380
}
@@ -127,7 +134,7 @@ mod test {
127134
pinocchio::pubkey::Pubkey,
128135
};
129136

130-
#[derive(Debug, PartialEq, Eq)]
137+
#[derive(Debug, PartialEq, Eq, Clone)]
131138
pub struct FooBar {
132139
pub key: Pubkey,
133140
pub amount: u64,
@@ -192,7 +199,7 @@ mod test {
192199
};
193200
let mut buffer = [0u8; FooBar::SERIALIZED_SIZE];
194201

195-
foo_bar.account_write_into(&mut buffer).unwrap();
202+
foo_bar.clone().account_write_into(&mut buffer).unwrap();
196203

197204
let decoded_foobar = FooBar::try_from_bytes(&buffer).unwrap();
198205

0 commit comments

Comments
 (0)