pub struct WritableView { /* private fields */ }Expand description
An AccountView that has been proven to be writable.
Guarantees that is_writable() == true without re-checking.
Implementations§
Source§impl WritableView
impl WritableView
Sourcepub fn validate(view: AccountView) -> Result<Self, ProgramError>
pub fn validate(view: AccountView) -> Result<Self, ProgramError>
Validate that the account is writable and return a capability token.
Sourcepub fn as_view(&self) -> &AccountView
pub fn as_view(&self) -> &AccountView
Access the underlying AccountView.
Sourcepub fn into_view(self) -> AccountView
pub fn into_view(self) -> AccountView
Consume and return the inner AccountView.
Methods from Deref<Target = AccountView>§
pub const SYSTEM_PROGRAM_ID: Address
Sourcepub unsafe fn owner(&self) -> &Address
pub unsafe fn owner(&self) -> &Address
The owning program’s address.
§Safety
The returned reference is invalidated if the account is assigned to a new owner or closed. The caller must ensure no concurrent mutation occurs.
Sourcepub fn is_writable(&self) -> bool
pub fn is_writable(&self) -> bool
Whether this account is writable in the transaction.
Sourcepub fn executable(&self) -> bool
pub fn executable(&self) -> bool
Whether this account contains an executable program.
Sourcepub fn resize_delta(&self) -> i32
pub fn resize_delta(&self) -> i32
Resize delta (difference between current and original data length).
Sourcepub fn is_data_empty(&self) -> bool
pub fn is_data_empty(&self) -> bool
Whether the account data is empty (data_len == 0).
Sourcepub fn set_lamports(&self, lamports: u64)
pub fn set_lamports(&self, lamports: u64)
Set the lamport balance.
Sourcepub fn owned_by(&self, program: &Address) -> bool
pub fn owned_by(&self, program: &Address) -> bool
Check whether this account is owned by the given program.
Sourcepub unsafe fn assign(&self, new_owner: &Address)
pub unsafe fn assign(&self, new_owner: &Address)
Assign a new owner.
§Safety
The caller must ensure the account is writable and that ownership transfer is authorized by the current owner program.
Sourcepub fn is_borrowed(&self) -> bool
pub fn is_borrowed(&self) -> bool
Whether the account data is currently borrowed (shared or exclusive).
Sourcepub fn is_borrowed_mut(&self) -> bool
pub fn is_borrowed_mut(&self) -> bool
Whether the account data is exclusively (mutably) borrowed.
Sourcepub fn check_borrow(&self) -> Result<(), ProgramError>
pub fn check_borrow(&self) -> Result<(), ProgramError>
Check that the account can be shared-borrowed.
Sourcepub fn check_borrow_mut(&self) -> Result<(), ProgramError>
pub fn check_borrow_mut(&self) -> Result<(), ProgramError>
Check that the account can be exclusively borrowed.
Sourcepub unsafe fn borrow_unchecked(&self) -> &[u8]
pub unsafe fn borrow_unchecked(&self) -> &[u8]
Borrow account data without borrow tracking.
§Safety
The caller must ensure no mutable borrow is active.
Sourcepub unsafe fn borrow_unchecked_mut(&self) -> &mut [u8]
pub unsafe fn borrow_unchecked_mut(&self) -> &mut [u8]
Mutably borrow account data without borrow tracking.
§Safety
The caller must ensure no other borrows (shared or exclusive) are active.
Sourcepub fn try_borrow(&self) -> Result<Ref<'_, [u8]>, ProgramError>
pub fn try_borrow(&self) -> Result<Ref<'_, [u8]>, ProgramError>
Try to obtain a shared borrow of the account data.
Returns Err(AccountBorrowFailed) if the data is exclusively borrowed.
Sourcepub fn try_borrow_mut(&self) -> Result<RefMut<'_, [u8]>, ProgramError>
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, [u8]>, ProgramError>
Try to obtain an exclusive (mutable) borrow of the account data.
Returns Err(AccountBorrowFailed) if the data is already borrowed.
Sourcepub fn segment_ref<T: Pod>(
&self,
offset: u32,
size: u32,
) -> Result<Ref<'_, T>, ProgramError>
pub fn segment_ref<T: Pod>( &self, offset: u32, size: u32, ) -> Result<Ref<'_, T>, ProgramError>
Project a typed segment from account data with native borrow tracking.
Sourcepub unsafe fn segment_ref_unchecked<T: Pod>(
&self,
offset: u32,
) -> Result<Ref<'_, T>, ProgramError>
pub unsafe fn segment_ref_unchecked<T: Pod>( &self, offset: u32, ) -> Result<Ref<'_, T>, ProgramError>
Acquire a shared segment borrow without size/bounds validation.
§Safety
The caller must have already verified:
offset + size_of::<T>()does not overflowoffset + size_of::<T>() <= data_len()
Sourcepub fn segment_mut<T: Pod>(
&self,
offset: u32,
size: u32,
) -> Result<RefMut<'_, T>, ProgramError>
pub fn segment_mut<T: Pod>( &self, offset: u32, size: u32, ) -> Result<RefMut<'_, T>, ProgramError>
Project a mutable typed segment from account data with native borrow tracking.
Sourcepub unsafe fn segment_mut_unchecked<T: Pod>(
&self,
offset: u32,
) -> Result<RefMut<'_, T>, ProgramError>
pub unsafe fn segment_mut_unchecked<T: Pod>( &self, offset: u32, ) -> Result<RefMut<'_, T>, ProgramError>
Acquire an exclusive segment borrow without size/bounds/writable validation.
§Safety
The caller must have already verified:
- The account is writable
offset + size_of::<T>()does not overflowoffset + size_of::<T>() <= data_len()
Sourcepub unsafe fn raw_ref<T: Pod>(&self) -> Result<Ref<'_, T>, ProgramError>
pub unsafe fn raw_ref<T: Pod>(&self) -> Result<Ref<'_, T>, ProgramError>
Explicit raw typed read of the account buffer.
Sourcepub unsafe fn raw_mut<T: Pod>(&self) -> Result<RefMut<'_, T>, ProgramError>
pub unsafe fn raw_mut<T: Pod>(&self) -> Result<RefMut<'_, T>, ProgramError>
Explicit raw typed write of the account buffer.
Sourcepub fn resize(&self, new_len: usize) -> Result<(), ProgramError>
pub fn resize(&self, new_len: usize) -> Result<(), ProgramError>
Resize the account data to new_len bytes.
Returns Err(InvalidRealloc) if the new length exceeds the
permitted increase from the original allocation.
Sourcepub unsafe fn resize_unchecked(&self, new_len: usize)
pub unsafe fn resize_unchecked(&self, new_len: usize)
Resize without bounds checking.
§Safety
The caller must guarantee new_len <= original_len + MAX_PERMITTED_DATA_INCREASE.
Sourcepub fn close(&self) -> ProgramResult
pub fn close(&self) -> ProgramResult
Close the account: zero lamports and data, reassign owner to the System Program.
§Caveat
This low-level routine does not verify the caller has
authority to close the account, Solana’s runtime enforces
owner/writable rules at transaction commit time regardless, but
higher-level APIs (e.g. hopper_runtime::AccountView::close_to)
should pre-check those rules. See account.rs::close_to for
the safe wrapper.
Sourcepub unsafe fn close_unchecked(&self)
pub unsafe fn close_unchecked(&self)
Sourcepub fn account_ptr(&self) -> *const RuntimeAccount
pub fn account_ptr(&self) -> *const RuntimeAccount
Raw pointer to the RuntimeAccount header.
Sourcepub fn require_signer(&self) -> ProgramResult
pub fn require_signer(&self) -> ProgramResult
Validate that this account is a signer, returning a typed error.
Sourcepub fn require_writable(&self) -> ProgramResult
pub fn require_writable(&self) -> ProgramResult
Validate that this account is writable.
Sourcepub fn require_owned_by(&self, program: &Address) -> ProgramResult
pub fn require_owned_by(&self, program: &Address) -> ProgramResult
Validate that this account is owned by the given program.
Sourcepub fn require_payer(&self) -> ProgramResult
pub fn require_payer(&self) -> ProgramResult
Validate signer + writable (common “payer” pattern).
Sourcepub fn disc(&self) -> u8
pub fn disc(&self) -> u8
Read the Hopper account discriminator (first byte of data).
Returns 0 if the account has no data.
Sourcepub fn version(&self) -> u8
pub fn version(&self) -> u8
Read the Hopper account version (second byte of data).
Returns 0 if the account has fewer than 2 bytes.
Sourcepub fn layout_id(&self) -> Option<&[u8; 8]>
pub fn layout_id(&self) -> Option<&[u8; 8]>
Read the 8-byte layout_id from the Hopper account header (bytes 4..12 of account data, per the canonical header format).
Returns None if the account has fewer than 12 bytes.
Sourcepub fn require_disc(&self, expected: u8) -> ProgramResult
pub fn require_disc(&self, expected: u8) -> ProgramResult
Verify that this account has the given discriminator.
Sourcepub fn check_signer(&self) -> Result<&Self, ProgramError>
pub fn check_signer(&self) -> Result<&Self, ProgramError>
Chainable signer check.
Sourcepub fn check_writable(&self) -> Result<&Self, ProgramError>
pub fn check_writable(&self) -> Result<&Self, ProgramError>
Chainable writable check.
Sourcepub fn check_owned_by(&self, program: &Address) -> Result<&Self, ProgramError>
pub fn check_owned_by(&self, program: &Address) -> Result<&Self, ProgramError>
Chainable ownership check.
Sourcepub fn check_disc(&self, expected: u8) -> Result<&Self, ProgramError>
pub fn check_disc(&self, expected: u8) -> Result<&Self, ProgramError>
Chainable discriminator check.
Sourcepub fn check_has_data(&self) -> Result<&Self, ProgramError>
pub fn check_has_data(&self) -> Result<&Self, ProgramError>
Chainable non-empty data check.
Sourcepub fn check_executable(&self) -> Result<&Self, ProgramError>
pub fn check_executable(&self) -> Result<&Self, ProgramError>
Chainable executable check.
Sourcepub fn check_address(&self, expected: &Address) -> Result<&Self, ProgramError>
pub fn check_address(&self, expected: &Address) -> Result<&Self, ProgramError>
Chainable address check.
Sourcepub fn check_data_len(&self, min_len: usize) -> Result<&Self, ProgramError>
pub fn check_data_len(&self, min_len: usize) -> Result<&Self, ProgramError>
Chainable minimum data length check.
Sourcepub fn read_owner(&self) -> Address
pub fn read_owner(&self) -> Address
Read the owner address as a copy (32-byte value).
Unlike owner() (which is unsafe due to reference invalidation
if assign() is called), this returns a copy that is always safe.
Costs 32 bytes of stack space but eliminates aliasing hazards.
Sourcepub fn flags(&self) -> u8
pub fn flags(&self) -> u8
Pack the account’s boolean flags into a single byte for fast comparison.
Bit layout:
- bit 0: is_signer
- bit 1: is_writable
- bit 2: executable
- bit 3: has data (data_len > 0)
Use with expect_flags() for single-instruction multi-check:
// Require: signer + writable + has data
account.expect_flags(0b1011)?;Sourcepub fn expect_flags(&self, required: u8) -> ProgramResult
pub fn expect_flags(&self, required: u8) -> ProgramResult
Check that the account’s flags contain all the required bits.
required is a bitmask of flags that must be set. See flags().
Trait Implementations§
Source§impl Clone for WritableView
impl Clone for WritableView
Source§fn clone(&self) -> WritableView
fn clone(&self) -> WritableView
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more