Migrate non-stable storage to stable storage on single canister

Im busy migrating non-stable storage data to stable storage on the same caninster. I already wiped my first canister by accident so I was wondering;

  • can non-stable and stable storage co-exist on a canister?
  • if not, how would i migrate the data the easiest way?
  • if so would non-stable memory where the data is restored by pre- post-upgrade conflict with stable memory?
pub static NON_STABLE_DATA: RefCell<Data> = RefCell::new(Data::default());
pub static STABLE_DATA: RefCell<StableCell<Data, Memory>> = RefCell::new(
            StableCell::init(
                MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0))),
                Data::default(),
            ).expect("failed")
        );

for the migration i had somehting like this

#[update]
pub fn migrate_to_stable() {
    let data = NON_STABLE_DATA.with(|d| d.borrow().clone());
    let _ = STABLE_DATA.with(|stable| {
        stable.borrow_mut().set(data)
    });
}

I’m by no means an expert here, my numerous stupid questions on the forum as proof :sweat_smile:. However, yes I can confirm that heap and stable memory can coexist.

An example illustrating this can be found in the stable structures library repository: https://github.com/dfinity/stable-structures/tree/main/examples/src/quick_start

Yes, we’ve written that example specifically for that purpose. The canisters holding the Bitcoin state, for instance, use the exact approach that’s in the quickstart example pointed to above.

Thanks thats a relief, going to give it an other try