1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#![no_std]

pub use soroban_sdk::*;
pub mod into_key;

pub use into_key::IntoKey;

/// Trait for loading and setting a singleton type
pub trait Lazy: Sized {
    fn get_lazy() -> Option<Self>;

    fn set_lazy(self);
}

static mut ENV: Option<Env> = None;

pub fn set_env(env: Env) {
    unsafe { ENV = Some(env) };
}

pub fn env() -> &'static Env {
    unsafe { ENV.as_ref().unwrap() }
}

impl<T> Lazy for T
where
    T: IntoKey + TryFromVal<Env, Val> + IntoVal<Env, Val>,
{
    fn get_lazy() -> Option<Self> {
        env().storage().persistent().get(&Self::into_key())
    }

    fn set_lazy(self) {
        env().storage().persistent().set(&Self::into_key(), &self);
    }
}

pub use loam_sdk_macro::{IntoKey, Lazy};