Skip to main content

Env

Struct Env 

Source
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>

Source

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 'a by code that uses the Env must not be accessed after 'a ends other than by parsing it, unless its contents accept every bit pattern. For example, a method that creates an Env to lend memory it received must give it a lifetime that lasts until the method returns, like the lifetime of the Env it received.
  • once the Env was given to code by reference, it must not be used afterward other than being dropped, since that code may have replaced it with another Env whose lifetime only lasts until it returns
Source

pub fn shard_index(&self) -> ShardIndex

Shard index where execution is happening

Source

pub fn own_address(&self) -> Address

Own address of the contract

Source

pub fn context<'b>(self: &'b &'b mut Self) -> Address

Context of the execution

Source

pub fn caller<'b>(self: &'b &'b mut Self) -> Address

Caller of this contract

Source

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.

Source

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.

Source

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§

Source§

impl<'a> Debug for Env<'a>

Source§

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

Formats the value using the given formatter. Read more

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> 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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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.