pub struct Env<'a> { /* private fields */ }Expand description
Ephemeral execution environment.
In guest environment equivalent to just EnvState, while on Unix and Windows an executor
context is also present
Implementations§
Source§impl<'a> Env<'a>
impl<'a> Env<'a>
Sourcepub unsafe fn with_executor_context(
state: EnvState,
executor_context: &'a dyn ExecutorContext,
) -> Self
pub unsafe fn with_executor_context( state: EnvState, executor_context: &'a dyn ExecutorContext, ) -> Self
Instantiate environment with executor context.
§Safety
Code that uses the returned Env may rely on its lifetime 'a: memory it borrows for 'a
(see Self::lend_output()) stays inaccessible to others until 'a ends, and contents it
leaves behind in that memory may not be a valid value. Hence:
- memory borrowed for
'aby code that uses theEnvmust not be accessed after'aends other than by parsing it, unless its contents accept every bit pattern. For example, a method that creates anEnvto lend memory it received must give it a lifetime that lasts until the method returns, like the lifetime of theEnvit received. - once the
Envwas given to code by reference, it must not be used afterward other than being dropped, since that code may have replaced it with anotherEnvwhose lifetime only lasts until it returns
Sourcepub fn shard_index(&self) -> ShardIndex
pub fn shard_index(&self) -> ShardIndex
Shard index where execution is happening
Sourcepub fn own_address(&self) -> Address
pub fn own_address(&self) -> Address
Own address of the contract
Sourcepub fn lend_output<'b, T>(&self, value: &'b mut T) -> Lent<'b, T>where
T: IoType,
'b: 'a,
pub fn lend_output<'b, T>(&self, value: &'b mut T) -> Lent<'b, T>where
T: IoType,
'b: 'a,
Lend an #[output] the method received, or a field of one, to calls as an #[output].
The memory stays borrowed until the method returns, so after a call its contents are only
accessible through the returned Lent, which parses them first. The state of the
contract (including the state that an #[init] method returns through its last
#[output]) and #[slot] and #[tmp] arguments are trusted without being parsed, so they
can’t be lent this way: lend a copy made with Lent::new() instead. Local memory is lent
with Lent::new(), Lent::<MaybeData<_>>::from_uninit() or
Lent::<VariableElements<_>>::from_uninit().
Code generated by #[contract] gives a method Env with a lifetime that covers the call of
the method, so the method must declare its #[output] with that lifetime to lend it:
#[update]
pub fn forward<'a>(
#[env] env: &mut Env<'a>,
#[output] mode: &'a mut Mode,
) -> Result<(), ContractError> {
let mut mode = env.lend_output(mode);
env.other_set_mode(MethodContext::Keep, OTHER_ADDRESS, &mut mode)?;
*mode.get_mut()? = Mode::B;
Ok(())
}With elided lifetimes (&mut Env<'_> and &mut Mode) compilation will fail with “lifetime
may not live long enough”, and rustc will suggest the signature above.
Sourcepub fn call<Args>(
&self,
contract: Address,
args: &mut Args,
method_context: MethodContext,
) -> Result<(), ContractError>where
Args: ExternalArgs,
pub fn call<Args>(
&self,
contract: Address,
args: &mut Args,
method_context: MethodContext,
) -> Result<(), ContractError>where
Args: ExternalArgs,
Call a method at specified address and with specified arguments.
This is a shortcut for PreparedMethod::new() + Self::call_prepared().
A call can leave contents behind in outputs that are not valid values of their types, even
when it succeeds, and it only initializes the return value, see ExternalArgs.
Sourcepub fn call_prepared(
&self,
method: PreparedMethod<'_>,
) -> Result<(), ContractError>
pub fn call_prepared( &self, method: PreparedMethod<'_>, ) -> Result<(), ContractError>
Call prepared method.
In most cases, this doesn’t need to be called directly. Extension traits provide a more convenient way to make method calls and are enough in most cases.
A call can leave contents behind in outputs that are not valid values of their types, even
when it succeeds, and it only initializes the return value, see ExternalArgs and
PreparedMethod::new_untyped().
Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Env<'a>
impl<'a> !Send for Env<'a>
impl<'a> !Sync for Env<'a>
impl<'a> !UnwindSafe for Env<'a>
impl<'a> Freeze for Env<'a>
impl<'a> Unpin for Env<'a>
impl<'a> UnsafeUnpin for Env<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more