loam_soroban_sdk/
lib.rs

1#![no_std]
2
3pub use soroban_sdk::*;
4pub mod into_key;
5pub mod loam_storage;
6
7pub use into_key::IntoKey;
8pub use loam_storage::*;
9
10/// Trait for loading and setting a singleton type
11pub trait Lazy: Sized {
12    fn get_lazy() -> Option<Self>;
13
14    fn set_lazy(self);
15}
16
17static mut ENV: Option<Env> = None;
18
19pub fn set_env(env: Env) {
20    unsafe { ENV = Some(env) };
21}
22
23/// Returns a reference to the current environment.
24///
25/// # Panics
26///
27/// This function will panic if the environment has not been initialized.
28/// It is expected that the environment is always initialized before this
29/// function is called in normal operation.
30#[must_use]
31#[allow(static_mut_refs)]
32pub fn env() -> &'static Env {
33    unsafe { ENV.as_ref().unwrap() }
34}
35
36impl<T> Lazy for T
37where
38    T: IntoKey + TryFromVal<Env, Val> + IntoVal<Env, Val>,
39{
40    fn get_lazy() -> Option<Self> {
41        env().storage().persistent().get(&Self::into_key())
42    }
43
44    fn set_lazy(self) {
45        env().storage().persistent().set(&Self::into_key(), &self);
46    }
47}
48
49pub use loam_sdk_macro::{IntoKey, Lazy};