gear_core::env

Struct PayloadSliceLock

Source
pub struct PayloadSliceLock { /* private fields */ }
Expand description

Lock for the payload of the incoming/currently executing message.

The type was mainly introduced to establish type safety mechanics for the read of the payload from externalities. To type’s purposes see Externalities::lock_payload docs.

§Usage

This type gives access to some slice of the currently executing message payload, but doesn’t do it directly. It gives to the caller the PayloadSliceAccess wrapper, which actually can return the slice of the payload. But this wrapper is instantiated only inside the Self::drop_with method. This is actually done to prevent a user of the type from locking payload of the message, which actually moves it, and forgetting to unlock it back, because if access to the slice buffer was granted directly from the holder, the type user could have written the data to memory and then have dropped the holder. As a result the executing message payload wouldn’t have been returned. So PayloadSliceLock::drop_with is a kind of scope-guard for the data and the PayloadSliceAccess is a data access guard.

For more usage info read Self::drop_with docs.

Implementations§

Source§

impl PayloadSliceLock

Source

pub fn try_new( (start, end): (u32, u32), msg_ctx: &mut MessageContext, ) -> Option<Self>

Creates a new PayloadSliceLock from the currently executed message context.

The method checks whether received range (slice) is correct, i.e., the end is lower than payload’s length. If the check goes well, the ownership over payload will be taken from the message context by mem::take.

Source

pub fn drop_with<JobErr, Job>(self, job: Job) -> DropPayloadLockBound<JobErr>
where Job: FnMut(PayloadSliceAccess<'_>) -> DropPayloadLockBound<JobErr>,

Uses the lock in the provided job and drops the lock after running it.

PayloadSliceLock’s main purpose is to provide safe access to the payload’s slice and ensure it will be returned back to the message.

Type docs explain how safe access is designed with PayloadSliceAccess.

We ensure that the payload is released back by returning the DropPayloadLockBound from the job. This type can actually be instantiated only from tuple of two: UnlockPayloadBound and some result with err variant type to be JobErr. The first is returned from Externalities::unlock_payload, so it means that that payload was reclaimed by the original owner. The other result stores actual error of the Job as it could have called fallible actions inside it. So, DropPayloadLockBound gives an opportunity to store the actual result of the job, but also gives guarantee that payload was reclaimed.

Auto Trait Implementations§

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> 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 = Infallible

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

impl<T> JsonSchemaMaybe for T