Struct soroban_sdk::storage::Storage
source · pub struct Storage { /* private fields */ }
Expand description
Storage stores and retrieves data for the currently executing contract.
All data stored can only be queried and modified by the contract that stores it. Contracts cannot query or modify data stored by other contracts.
Storage has persistent and temporary modes.
For persistent mode data is stored in the ledger and is viewable outside of contracts wherever the ledger is accessible. This is the most universally useful storage mode.
For temporary mode data exists only during the execution time of the top-level contract. It is thus only useful to store data between several cross-contract calls (e.g. to increase and then spend the token allowance from another contract).
Examples
use soroban_sdk::{Env, Symbol};
let storage = env.storage();
let key = Symbol::short("key");
env.storage().set(&key, &1);
assert_eq!(storage.has(&key), true);
assert_eq!(storage.get::<_, i32>(&key), Some(Ok(1)));
Implementations§
source§impl Storage
impl Storage
sourcepub fn has<K>(&self, key: &K) -> boolwhere
K: IntoVal<Env, RawVal>,
pub fn has<K>(&self, key: &K) -> boolwhere K: IntoVal<Env, RawVal>,
Returns if there is a value stored for the given key in the currently executing contracts storage.
sourcepub fn get<K, V>(&self, key: &K) -> Option<Result<V, V::Error>>where
V::Error: Debug,
K: IntoVal<Env, RawVal>,
V: TryFromVal<Env, RawVal>,
pub fn get<K, V>(&self, key: &K) -> Option<Result<V, V::Error>>where V::Error: Debug, K: IntoVal<Env, RawVal>, V: TryFromVal<Env, RawVal>,
Returns the value stored for the given key in the currently executing contract’s storage, when present.
Returns None
when the value is missing.
If the value is present, then the returned value will be a result of
converting the internal value representation to V
.
sourcepub fn get_unchecked<K, V>(&self, key: &K) -> Result<V, V::Error>where
V::Error: Debug,
K: IntoVal<Env, RawVal>,
V: TryFromVal<Env, RawVal>,
pub fn get_unchecked<K, V>(&self, key: &K) -> Result<V, V::Error>where V::Error: Debug, K: IntoVal<Env, RawVal>, V: TryFromVal<Env, RawVal>,
Returns the value there is a value stored for the given key in the currently executing contract’s storage.
The returned value is a result of converting the internal value
representation to V
.
Panics
When the key does not have a value stored.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Storage
impl !Send for Storage
impl !Sync for Storage
impl Unpin for Storage
impl !UnwindSafe for Storage
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere
C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
impl<T, U, V, E, C> Compare<(T, U, V)> for Cwhere C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.