pub struct Anchor(/* private fields */);bank only.Expand description
Proof that the holder runs in bank 0 (the always-mapped region), and
so may run a closure across a bank switch via scope / there.
Those operations lend a closure across a switch; the closure’s code must stay
mapped while it runs, which only holds for code in bank 0. An Anchor is that
guarantee, as a zero-sized Copy capability. The #[bank::main] and
#[bank::zero] macros mint one at the top of every body (their code is
in bank 0); a #[bank] function gets none, so it cannot reach scope / there,
which would otherwise run a banked closure the switch unmaps.
Being Copy, it threads into nested scopes without borrow friction.
Implementations§
Source§impl Anchor
impl Anchor
Sourcepub const unsafe fn assume() -> Anchor
pub const unsafe fn assume() -> Anchor
Assert that the calling code runs in bank 0.
§Safety
The caller must physically reside in bank 0 (the always-mapped region): a
#[bank::main] / #[bank::zero] body, or a plain fn the linker keeps
in bank 0. A #[bank] function is not in bank 0 and must never mint one.
An Anchor must also not be moved into #[bank] code: it witnesses that the
current code is in bank 0, which carrying it into a banked function would
falsify. (Safe code cannot do this: the macro-minted Anchor is hygienic and
there is no other safe constructor.)