@@ -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