Skip to main content

Lent

Struct Lent 

Source
pub struct Lent<'a, T>
where T: IoType,
{ /* private fields */ }
Expand description

Memory lent to calls as an #[output], whose contents are parsed before they are accessed.

A call can leave contents behind that are not a valid value, for example when it fails halfway, so the contents are only accessible through Self::get_mut(), which parses them once after each call. Plain values can also be replaced with Self::set(). Memory whose contents accept every bit pattern can be given to calls directly, see OutputArg.

Created with Self::new() for a local value, with Lent::<MaybeData<_>>::from_uninit() or Lent::<VariableElements<_>>::from_uninit() for a local container, and with Env::lend_output() for an #[output] the method received.

Implementations§

Source§

impl<T> Lent<'static, T>
where T: TrivialType,

Source

pub fn new(value: T) -> Self

Lend a local value

Source§

impl<'a, Data> Lent<'a, MaybeData<Data>>
where Data: TrivialType, MaybeData<Data>: IoType,

Source

pub fn from_uninit( uninit: &'a mut MaybeUninit<Data>, size: &'a mut u32, ) -> Option<Self>

Lend a local [MaybeData] over uninitialized memory, like [MaybeData::from_uninit()].

Returns None if size != 0.

Source§

impl<'a, Element, const RECOMMENDED_ALLOCATION: u32> Lent<'a, VariableElements<Element, RECOMMENDED_ALLOCATION>>
where Element: TrivialType, VariableElements<Element, RECOMMENDED_ALLOCATION>: IoType,

Source

pub fn from_uninit( uninit: &'a mut [MaybeUninit<Element>], size: &'a mut u32, ) -> Option<Self>

Lend a local [VariableElements] over uninitialized memory, like [VariableElements::from_uninit()].

Returns None if size != 0 or if the buffer is larger than u32::MAX bytes.

Source§

impl<'a, T> Lent<'a, T>
where T: IoType,

Source

pub unsafe fn from_mut_unchecked(value: &'a mut T) -> Self

Lend value, which is only borrowed for 'a, rather than until the method returns like with Env::lend_output().

§Safety

After the returned Lent was given to a call, value may hold contents that are not a valid value. Once the Lent is no longer used, the contents of value must not be accessed without being parsed first, unless the last use of the Lent after its last call was a successful Self::get_mut() or Self::set(), or the contents of T accept every bit pattern.

Source

pub fn get_mut(&mut self) -> Result<&mut T, ContractError>

Exclusive access to the contents, which are parsed once after each call.

Repeated calls without a call in between don’t parse the contents again. Returns ContractError::BadOutput if they are not a valid value of T. They then stay inaccessible until Self::set(), which only exists for plain values, replaces them. Lending them again doesn’t help either: methods generated by #[contract] reject outputs with invalid initial contents with ContractError::BadInput.

Source§

impl<T> Lent<'_, T>
where T: TrivialType,

Source

pub fn set(&mut self, value: T) -> &mut T

Replace the contents with a valid value

Trait Implementations§

Source§

impl<T> Debug for Lent<'_, T>
where T: IoType,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> OutputArg<T> for Lent<'_, T>
where T: IoType,

Source§

fn as_raw_output(&mut self) -> (NonNull<T::PointerType>, u32, u32)

Pointer to the contents, size and capacity to give to a call
Source§

unsafe fn set_output_size(&mut self, size: u32)

Set the size that a successful call reported. Read more

Auto Trait Implementations§

§

impl<'a, T> !Send for Lent<'a, T>

§

impl<'a, T> !Sync for Lent<'a, T>

§

impl<'a, T> !UnwindSafe for Lent<'a, T>

§

impl<'a, T> Freeze for Lent<'a, T>
where LentValue<'a, T>: Freeze, NonNull<<T as IoType>::PointerType>: Freeze,

§

impl<'a, T> RefUnwindSafe for Lent<'a, T>
where LentValue<'a, T>: RefUnwindSafe, NonNull<<T as IoType>::PointerType>: RefUnwindSafe,

§

impl<'a, T> Unpin for Lent<'a, T>
where LentValue<'a, T>: Unpin, NonNull<<T as IoType>::PointerType>: Unpin,

§

impl<'a, T> UnsafeUnpin for Lent<'a, T>
where LentValue<'a, T>: UnsafeUnpin, NonNull<<T as IoType>::PointerType>: UnsafeUnpin,

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.