Migrating to the latest version of ic_stable_structures panics due to max_size
being greater than actual size of the struct. I have explained the problem in detail below
I had a struct WasmType
which looked like this:
#[derive(Clone, Serialize, Deserialize, PartialEq, PartialOrd, Eq, Ord, CandidType)]
pub enum WasmType {
SubnetOrchestratorWasm,
IndividualUserWasm,
PostCacheWasm
}
impl Storable for WasmType {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
let mut bytes = vec![];
ciborium::ser::into_writer(self, &mut bytes).unwrap();
Cow::Owned(bytes)
}
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
let wasm_type: WasmType = de::from_reader(bytes.as_ref()).unwrap();
wasm_type
}
}
impl BoundedStorable for WasmType {
const MAX_SIZE: u32 = 100;
const IS_FIXED_SIZE: bool = true;
}
This worked perfectly fine.
Once I migrated to latest version of ic-stable-structures (0.6.2) I reimplemented the trait Storable
to include Bound
and removed the implementation of BoundedStorable
impl Storable for WasmType {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
let mut bytes = vec![];
ciborium::ser::into_writer(self, &mut bytes).unwrap();
Cow::Owned(bytes)
}
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
let wasm_type: WasmType = de::from_reader(bytes.as_ref()).unwrap();
wasm_type
}
const BOUND: Bound = Bound::Bounded { max_size: 100, is_fixed_size: true };
}
Using this again with stable_structures panics with error
Panicked at 'assertion `left == right` failed: expected a fixed-size element with length 100 bytes, but found 19 bytes\n
isn’t max_size
an upper bound for the size of the struct?