Skip to main content

AccountView

Struct AccountView 

Source
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 raw pointer must be valid and point to memory containing a RuntimeAccount struct, 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

Source

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.

Source

pub fn address(&self) -> &Address

Address of the account.

Source

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.

Source

pub fn is_signer(&self) -> bool

Indicate whether the transaction was signed by this account.

Source

pub fn is_writable(&self) -> bool

Indicate whether the account is writable or not.

Source

pub fn executable(&self) -> bool

Indicate whether this account represents an executable program or not.

Source

pub fn data_len(&self) -> usize

Return the size of the account data.

Source

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.

Source

pub fn lamports(&self) -> u64

Return the lamports in the account.

Source

pub fn set_lamports(&self, lamports: u64)

Set the lamports in the account.

Source

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.

Source

pub fn owned_by(&self, program: &Address) -> bool

Checks if the account is owned by the given program.

Source

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.

Source

pub fn is_borrowed(&self) -> bool

Return true if the account data is borrowed in any form.

Source

pub fn is_borrowed_mut(&self) -> bool

Return true if the account data is mutably borrowed.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const fn account_ptr(&self) -> *const RuntimeAccount

Returns the raw pointer to the Account struct.

Source

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

Source§

fn clone(&self) -> AccountView

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AccountView

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for AccountView

Source§

fn eq(&self, other: &AccountView) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for AccountView

Source§

impl Eq for AccountView

Source§

impl StructuralPartialEq for AccountView

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.