Hi all
I am trying to introduce the new stable structures in order to store a complex struct.
I have read the custom example in the dfinity repo, and it looks like this:
static MAP: RefCell<StableBTreeMap<u64, UserProfile, Memory>> = RefCell::new(
StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0))),
)
);
My problem is, that the “outer-most” container is not a StableBTreeMap, but a struct like:
pub struct UserRegistry {
users: BTreeMap<PrincipalStorable, User>,
}
The struct PrincipleStorable basically wraps the Principal in order to make it “storable”.
How can I use this UserRegistry in my thread local environment?
I have tried to use the Cell API provided by stable structs:
use ic_stable_structures::{[...] cell::Cell as StableCell};
static USER_REGISTRY: RefCell<StableCell<UserRegistry, Memory>> = RefCell::new(StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0))),
<UserRegistry>::new()).unwrap());
and later in the code, when I modify the user registry, i try this:
USER_REGISTRY.with(
|ur| -> Result<User, SmartVaultErr> {
let mut user_registry_snapshot = ur.borrow().get().clone();
// ...
user_registry_snapshot.add_user(new_user) {...};
ur.borrow_mut().set(user_registry_snapshot){...}
},
)
It is quite a workaround… the code compiles, but it does not work as planned.
Any clue what I am doing wrong? Does the user field in the UserRegistry be of type StableBTreeMap as well? Any other thoughts?
Thank you