Skip to main content

NNInt

Struct NNInt 

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

A stored integer with a commit-time floor, not a call-time non-negativity invariant.

The value is set + add: set is the last explicit set (or the value carried from the previous transaction) and add is what inc has accumulated this transaction. The storage layer enforces, at commit, that set + add >= min(set, 0):

  • With a non-negative (or never-)set, the floor is 0: the value can’t be driven below zero, and a transaction that would commit a negative value reverts.
  • You may set a negative value; the floor becomes that value. The counter can hold it, but increments still can’t take it any lower than the value you set.

Crucially, for inc this is a commit-time constraint, not a call-time error. For a hard, call-time “never negative” check with a clear error (e.g. an ERC20 debit), use strict_inc / try_strict_inc. The exact rules (and concurrency constraints) are on the non_negative_int_* free functions in sys_exn::nnint.

Implementations§

Source§

impl NNInt

Source

pub fn get(&self) -> i128

Get the current value. Aborts if location not yet set.

Source

pub fn try_get(&self) -> Result<i128, LibraryExn>

Get the current value, reporting failure as Err instead of aborting (the try_ law; see the exn module docs). A not-yet-set location is an Err for which is_storage_missing is true — use none_if_missing to treat just that case as an ordinary None. See non_negative_int_try_get for the full error contract.

Source

pub fn set(&mut self, set_value: i128)

Set the value, clearing accumulated incs this transaction.

A negative set_value is allowed: the commit-time floor becomes that value, so the counter can hold it but increments still can’t take it any lower. A non-negative set_value keeps the floor at 0. (At commit, value >= min(set_value, 0) must hold.) See the type docs.

Source

pub fn inc(&mut self, increment: i128)

Increment by an amount (without querying the current value).

The floor is enforced at commit, not here: the transaction reverts if the committed value would fall below min(set, 0) (see the type docs). For a call-time non-negativity check, use strict_inc. Throws on overflow.

Source

pub fn strict_inc(&mut self, increment: i128)

Increment by an amount, throwing in case of a negative result.

Source

pub fn try_strict_inc(&mut self, increment: i128) -> Result<(), LibraryExn>

Increment by an amount, like NNInt::strict_inc, but report failure to the caller instead of aborting (the try_ law; see the exn module docs), so a contract can raise a domain-specific exception. On Err the stored value is unmodified; no extra storage roundtrip over NNInt::inc. The domain errors are NonNegativeIntOverflow/NonNegativeIntUnderflow; a wrong storage type at the key or any other syscall failure surfaces as LibraryExn::Syscall.

Trait Implementations§

Source§

impl Debug for NNInt

Source§

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

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

impl Deletable for NNInt

Source§

fn delete_internal(&mut self, exclusive: bool)

Delete storage for this node. Read more
Source§

fn delete(&mut self)

Delete using the keypath’s own locking semantics.
Source§

fn delete_exclusive(&mut self)

Delete, then request an exclusive lock, regardless of keypath flag. Later writes in the transaction keep the lock.
Source§

impl Flaggable for NNInt

Source§

fn flags(&self) -> StorageFlags

The storage flags the current transaction requests for this key, as seen by the current call frame: ExclusiveLock once an exclusive write or set_flags has asked for it, until clear_flags. A key with no pending write has none.
Source§

fn set_flags(&mut self, flags: StorageFlags)

Adds flags (a bitwise OR) to the key’s storage flags. Later writes through any handle keep them. Read more
Source§

fn clear_flags(&mut self)

Removes every storage flag from the key. Does nothing if no write to it is pending.
Source§

impl From<Keypath> for NNInt

Source§

fn from(keypath: Keypath) -> Self

Converts to this type from the input type.
Source§

impl StorageType for NNInt

Auto Trait Implementations§

§

impl Freeze for NNInt

§

impl RefUnwindSafe for NNInt

§

impl Send for NNInt

§

impl Sync for NNInt

§

impl Unpin for NNInt

§

impl UnsafeUnpin for NNInt

§

impl UnwindSafe for NNInt

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> ContractState for T
where T: From<Keypath>,

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

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, !>

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.