Allocate stable memory IDs safely in a Rust canister
A MemoryManager divides stable memory into virtual memories. Each persistent structure needs a permanent assignment that survives Rust refactoring and upgrades.
Treat assignments as storage schema
Version one keeps users in memory 0 and balances in memory 1. Add a new audit index at an unused ID. Reordering an enum that assigns implicit discriminants can accidentally reopen balances as another structure. Declare explicit IDs and document any additional memories required by a multi-memory structure.
MemoryId(0) → users B-tree
MemoryId(1) → balances B-tree
MemoryId(2) → new audit log index
Exercise an upgrade with old bytes
Install the previous release, write a known user and balance, then upgrade and read both values before inserting new records. Stable storage preserves bytes. A change to key ordering, serialization or size bounds can still break decoding even when memory IDs match.