pub struct AccountView { /* private fields */ }Expand description
Wrapper struct for a RuntimeAccount.
This struct provides safe access to the data in a RuntimeAccount.
It is also used to track borrows of the account data, given that
an account can be “shared” across multiple AccountView instances.
§Invariants
- The
rawpointer must be valid and point to memory containing aRuntimeAccountstruct, immediately followed by the account’s data region. - The length of the account data must exactly match the value stored in
RuntimeAccount::data_len.
These conditions must always hold for any AccountView created from
a raw pointer.
Implementations§
Source§impl AccountView
impl AccountView
Sourcepub unsafe fn new_unchecked(raw: *mut RuntimeAccount) -> AccountView
pub unsafe fn new_unchecked(raw: *mut RuntimeAccount) -> AccountView
Creates a new AccountView for a given raw account pointer.
§Safety
The caller must ensure that the raw pointer is valid and points
to memory containing a RuntimeAccount struct, immediately followed by
the account’s data region.
Sourcepub unsafe fn owner(&self) -> &Address
pub unsafe fn owner(&self) -> &Address
Return a reference to the address of the program that owns this account.
For ownership checks, use the safe owned_by method instead.
§Safety
This method is unsafe because it returns a reference to the owner field,
which can be modified by assign and close methods. It is undefined
behavior to use this reference after the account owner has been modified.
Sourcepub fn is_writable(&self) -> bool
pub fn is_writable(&self) -> bool
Indicate whether the account is writable or not.
Sourcepub fn executable(&self) -> bool
pub fn executable(&self) -> bool
Indicate whether this account represents an executable program or not.
Sourcepub fn resize_delta(&self) -> i32
pub fn resize_delta(&self) -> i32
Return the delta between the original data length and the current data length.
This value will be different than zero if the account has been resized during the current instruction.
Sourcepub fn set_lamports(&self, lamports: u64)
pub fn set_lamports(&self, lamports: u64)
Set the lamports in the account.
Sourcepub fn is_data_empty(&self) -> bool
pub fn is_data_empty(&self) -> bool
Indicates whether the account data is empty or not.
An account is considered empty if the data length is zero.
Sourcepub fn owned_by(&self, program: &Address) -> bool
pub fn owned_by(&self, program: &Address) -> bool
Checks if the account is owned by the given program.
Sourcepub unsafe fn assign(&self, new_owner: &Address)
pub unsafe fn assign(&self, new_owner: &Address)
Changes the owner of the account.
§Safety
It is undefined behavior to use this method while there is an active reference
to the owner returned by Self::owner.
Sourcepub fn is_borrowed(&self) -> bool
pub fn is_borrowed(&self) -> bool
Return true if the account data is borrowed in any form.
Sourcepub fn is_borrowed_mut(&self) -> bool
pub fn is_borrowed_mut(&self) -> bool
Return true if the account data is mutably borrowed.
Sourcepub unsafe fn borrow_unchecked(&self) -> &[u8]
pub unsafe fn borrow_unchecked(&self) -> &[u8]
Returns an immutable reference to the data in the account.
§Safety
This method is unsafe because it does not return a Ref, thus leaving the borrow
flag untouched. Useful when an instruction has verified non-duplicate accounts.
Sourcepub unsafe fn borrow_unchecked_mut(&self) -> &mut [u8]
pub unsafe fn borrow_unchecked_mut(&self) -> &mut [u8]
Returns a mutable reference to the data in the account.
§Safety
This method is unsafe because it does not return a RefMut, thus leaving the borrow
flag untouched. Useful when an instruction has verified non-duplicate accounts.
Sourcepub fn try_borrow(&self) -> Result<Ref<'_, [u8]>, ProgramError>
pub fn try_borrow(&self) -> Result<Ref<'_, [u8]>, ProgramError>
Tries to get an immutable reference to the account data, failing if the account is already mutably borrowed.
Sourcepub fn try_borrow_mut(&self) -> Result<RefMut<'_, [u8]>, ProgramError>
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, [u8]>, ProgramError>
Tries to get a mutable reference to the account data, failing if the account is already borrowed in any form.
Sourcepub fn check_borrow(&self) -> Result<(), ProgramError>
pub fn check_borrow(&self) -> Result<(), ProgramError>
Check if it is possible to get an immutable reference to the account data, failing if the account is already mutably borrowed or there are not enough immutable borrows available.
Sourcepub fn check_borrow_mut(&self) -> Result<(), ProgramError>
pub fn check_borrow_mut(&self) -> Result<(), ProgramError>
Checks if it is possible to get a mutable reference to the account data, failing if the account is already borrowed in any form.
Sourcepub fn resize(&self, new_len: usize) -> Result<(), ProgramError>
pub fn resize(&self, new_len: usize) -> Result<(), ProgramError>
Resize (either truncating or zero extending) the account’s data.
The account data can be increased by up to MAX_PERMITTED_DATA_INCREASE bytes
within an instruction.
§Important
This method makes assumptions about the layout and location of memory
referenced by RuntimeAccount fields. It should only be called for
instances of AccountView that were created by the runtime and received
in the process_instruction entrypoint of a program.
Sourcepub unsafe fn resize_unchecked(
&self,
new_len: usize,
) -> Result<(), ProgramError>
pub unsafe fn resize_unchecked( &self, new_len: usize, ) -> Result<(), ProgramError>
Resize (either truncating or zero extending) the account’s data.
The account data can be increased by up to MAX_PERMITTED_DATA_INCREASE bytes
§Safety
This method is unsafe because it does not check if the account data is already borrowed. The caller must guarantee that there are no active borrows to the account data.
Sourcepub fn close(&self) -> Result<(), ProgramError>
pub fn close(&self) -> Result<(), ProgramError>
Zero out the the account’s data length, lamports and owner fields, effectively closing the account.
Note: This does not zero the account data. The account data will be zeroed by the runtime at the end of the instruction where the account was closed or at the next CPI call.
§Important
The lamports must be moved from the account prior to closing it to prevent an unbalanced instruction error. Any existing reference to the account owner will be invalidated after calling this method.
Sourcepub unsafe fn close_unchecked(&self)
pub unsafe fn close_unchecked(&self)
Zero out the the account’s data length, lamports and owner fields, effectively closing the account.
Note: This does not zero the account data. The account data will be zeroed by the runtime at the end of the instruction where the account was closed or at the next CPI call.
§Important
The lamports must be moved from the account prior to closing it to prevent an unbalanced instruction error.
If Self::resize is called after closing the account, it might incorrectly
return an error for going over the limit if the account previously had space
allocated since this method does not update the Self::resize_delta value.
§Safety
This method is unsafe because it does not check if the account data is already borrowed. It should only be called when the account is not being used.
It also makes assumptions about the layout and location of memory
referenced by RuntimeAccount fields. It should only be called for
instances of AccountView that were created by the runtime and received
in the process_instruction entrypoint of a program.
Sourcepub const fn account_ptr(&self) -> *const RuntimeAccount
pub const fn account_ptr(&self) -> *const RuntimeAccount
Returns the raw pointer to the Account struct.
Sourcepub fn data_ptr(&self) -> *mut u8
pub fn data_ptr(&self) -> *mut u8
Returns the memory address of the account data.
§Important
Obtaining the raw pointer itself is safe, but de-referencing it requires
the caller to uphold Rust’s aliasing rules. It is undefined behavior to
de-reference the pointer or write through it while any safe reference
(e.g., from any of borrow or borrow_mut methods) to the same data
is still alive.
Trait Implementations§
Source§impl Clone for AccountView
impl Clone for AccountView
Source§fn clone(&self) -> AccountView
fn clone(&self) -> AccountView
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more