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 is0: the value can’t be driven below zero, and a transaction that would commit a negative value reverts. - You may
seta 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
impl NNInt
Sourcepub fn try_get(&self) -> Result<i128, LibraryExn>
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.
Sourcepub fn set(&mut self, set_value: i128)
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.
Sourcepub fn inc(&mut self, increment: i128)
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.
Sourcepub fn strict_inc(&mut self, increment: i128)
pub fn strict_inc(&mut self, increment: i128)
Increment by an amount, throwing in case of a negative result.
Sourcepub fn try_strict_inc(&mut self, increment: i128) -> Result<(), LibraryExn>
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 Deletable for NNInt
impl Deletable for NNInt
Source§fn delete_internal(&mut self, exclusive: bool)
fn delete_internal(&mut self, exclusive: bool)
Source§fn delete_exclusive(&mut self)
fn delete_exclusive(&mut self)
Source§impl Flaggable for NNInt
impl Flaggable for NNInt
Source§fn flags(&self) -> StorageFlags
fn flags(&self) -> StorageFlags
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)
fn set_flags(&mut self, flags: StorageFlags)
flags (a bitwise OR) to the key’s storage flags. Later writes
through any handle keep them. Read more