thruput_sdk/storage/flags.rs
1//! Storage flags on leaf handles.
2//!
3//! A handle's exclusive access mode (from `at_mut_exclusive`, `set_exclusive`
4//! or `delete_exclusive`) is a write policy: each successful write through it
5//! is followed by a request for `ExclusiveLock`. Building, reading and
6//! dropping a handle leave the host's flags alone, and writes through an
7//! ordinary handle never remove them, so a lock taken once lasts for the rest
8//! of the transaction unless [`clear_flags`](Flaggable::clear_flags) removes
9//! it. See [`sys_result::flags`](crate::sys_result::flags).
10
11use super::sealed::Sealed;
12use crate::StorageFlags;
13use crate::types::StorageKey;
14
15/// A leaf handle whose key's storage flags can be read and changed.
16///
17/// Implemented by `Value`, `NNInt`, `Balance` and `Bytestring`. Sealed, like
18/// [`StorageType`](super::StorageType).
19#[allow(private_bounds)]
20pub trait Flaggable: Sealed {
21 /// The storage key the flags belong to.
22 #[doc(hidden)]
23 fn storage_key(&self) -> &StorageKey;
24
25 /// The storage flags the current transaction requests for this key, as
26 /// seen by the current call frame:
27 /// [`ExclusiveLock`](StorageFlags::ExclusiveLock) once an exclusive write
28 /// or [`set_flags`](Self::set_flags) has asked for it, until
29 /// [`clear_flags`](Self::clear_flags). A key with no pending write has
30 /// none.
31 fn flags(&self) -> StorageFlags {
32 crate::sys_exn::flags::storage_flags(self.storage_key())
33 }
34
35 /// Adds `flags` (a bitwise OR) to the key's storage flags. Later writes
36 /// through any handle keep them.
37 ///
38 /// Throws unless a write to the key is pending in the current call frame:
39 /// one made here, or inherited from a caller or a successful callee. A
40 /// value that only exists from an earlier block does not count.
41 fn set_flags(&mut self, flags: StorageFlags) {
42 crate::sys_exn::flags::set_storage_flags(self.storage_key(), flags)
43 }
44
45 /// Removes every storage flag from the key. Does nothing if no write to
46 /// it is pending.
47 fn clear_flags(&mut self) {
48 crate::sys_exn::flags::clear_storage_flags(self.storage_key())
49 }
50}