From 268ac770b6f2c813fc94d1d1d1cfd3b5c402e93b Mon Sep 17 00:00:00 2001 From: brooks Date: Fri, 8 Dec 2023 14:11:48 -0500 Subject: [PATCH] Implements ReadableAccount for RefMut Account and AccountSharedData --- sdk/src/account.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/sdk/src/account.rs b/sdk/src/account.rs index d94d7dc2c6b6e5..951f5dd3c64a25 100644 --- a/sdk/src/account.rs +++ b/sdk/src/account.rs @@ -16,7 +16,7 @@ use { }, solana_program::{account_info::AccountInfo, debug_account_data::*, sysvar::Sysvar}, std::{ - cell::{Ref, RefCell}, + cell::{Ref, RefCell, RefMut}, fmt, mem::MaybeUninit, ptr, @@ -351,6 +351,34 @@ impl ReadableAccount for Ref<'_, AccountSharedData> { } } +impl ReadableAccount for RefMut<'_, AccountSharedData> { + fn lamports(&self) -> u64 { + self.lamports + } + fn data(&self) -> &[u8] { + &self.data + } + fn owner(&self) -> &Pubkey { + &self.owner + } + fn executable(&self) -> bool { + self.executable + } + fn rent_epoch(&self) -> Epoch { + self.rent_epoch + } + fn to_account_shared_data(&self) -> AccountSharedData { + AccountSharedData { + lamports: self.lamports(), + // avoid data copy here + data: Arc::clone(&self.data), + owner: *self.owner(), + executable: self.executable(), + rent_epoch: self.rent_epoch(), + } + } +} + impl ReadableAccount for Ref<'_, Account> { fn lamports(&self) -> u64 { self.lamports @@ -369,6 +397,24 @@ impl ReadableAccount for Ref<'_, Account> { } } +impl ReadableAccount for RefMut<'_, Account> { + fn lamports(&self) -> u64 { + self.lamports + } + fn data(&self) -> &[u8] { + &self.data + } + fn owner(&self) -> &Pubkey { + &self.owner + } + fn executable(&self) -> bool { + self.executable + } + fn rent_epoch(&self) -> Epoch { + self.rent_epoch + } +} + fn debug_fmt(item: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut f = f.debug_struct("Account");