Skip to main content

hopper_core/accounts/
unchecked.rs

1//! Unchecked account wrapper -- no validation at construction.
2//!
3//! Used for accounts whose validity is the program's responsibility.
4//! Passes through the raw AccountView for arbitrary inspection.
5
6use hopper_runtime::{AccountView, Address};
7
8/// An unchecked account. No validation is performed.
9///
10/// Use when the program must inspect the account manually before deciding
11/// what to do (e.g. conditional logic based on owner or data).
12#[derive(Clone, Copy)]
13pub struct UncheckedAccount<'a> {
14    view: &'a AccountView,
15}
16
17impl<'a> UncheckedAccount<'a> {
18    /// Wrap an account without validation.
19    #[inline(always)]
20    pub fn new(account: &'a AccountView) -> Self {
21        Self { view: account }
22    }
23
24    /// The account's address.
25    #[inline(always)]
26    pub fn address(&self) -> &Address {
27        self.view.address()
28    }
29
30    /// The underlying AccountView.
31    #[inline(always)]
32    pub fn to_account_view(&self) -> &'a AccountView {
33        self.view
34    }
35
36    /// Whether the account is a signer.
37    #[inline(always)]
38    pub fn is_signer(&self) -> bool {
39        self.view.is_signer()
40    }
41
42    /// Whether the account is writable.
43    #[inline(always)]
44    pub fn is_writable(&self) -> bool {
45        self.view.is_writable()
46    }
47
48    /// The account owner.
49    ///
50    /// # Safety
51    ///
52    /// Caller must ensure no conflicting mutable borrows on the account.
53    #[inline(always)]
54    pub unsafe fn owner(&self) -> &Address {
55        // SAFETY: Caller guarantees no conflicting borrows.
56        unsafe { self.view.owner() }
57    }
58}