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
#[macro_export]
macro_rules! managed {
    ($type:ident) => {
        static _MANAGED_ROOT_PATH: std::sync::OnceLock<std::path::PathBuf> = std::sync::OnceLock::new();
        static mut STORAGE: *mut rtools::data_manager::DataStorage<$type> = std::ptr::null_mut();

        impl rtools::data_manager::Managed for $type {}

        impl rtools::data_manager::DataManager<$type> for $type {
            fn root_path() -> &'static std::path::Path {
                _MANAGED_ROOT_PATH.get().expect(&format!(
                    "Managed root path for type {} is not set.",
                    stringify!($type)
                ))
            }

            fn set_root_path(path: &std::path::Path) {
                _MANAGED_ROOT_PATH.set(path.to_path_buf()).expect(&format!(
                    "Managed root path for type {} was already set set.",
                    stringify!($type)
                ))
            }

            fn storage() -> &'static mut rtools::data_manager::DataStorage<$type> {
                unsafe {
                    if STORAGE.is_null() {
                        STORAGE = Box::into_raw(Box::new(Default::default()));
                    }
                    STORAGE.as_mut().unwrap()
                }
            }
        }
    };
}