I define const MAX_SIZE: u32 = (size_of::<String>() as u32 * 2) + 29; which equals 77 but, when I compile the canister and give a try I get an error message Expected <= 53 bytes, found 82 bytes at runtime.
Are you possibly looking at the textual encoding of the principal rather than the raw value? Or maybe it’s the candid encoding? I know if I want the raw principal value as a blob I have to do Principal.toBlob() in Motoko.
I get 77 as a result. However, when I use the exact same code - (size_of::<String>() as u32 * 2) + 29 - to define my implementation for my struct in my stable-structures, I get the error max size limit 53 at runtime.
Been coding since 6am today and it’s 10pm, so I probably need to go to sleep and have another look tomorrow again, which I’ll do .
I don’t use rust so I’m not sure exactly what’s going on but If I’m understanding correctly it looks like there’s a 5 byte difference (82 - 77) between what you expect the size to be and the actual size of the serialized structure. I brought up candid encoding before because it adds some bytes to the data being encoded. I believe it adds characters ”DIDL” plus a type tag and was wondering if that could be the 5 bytes that isn’t being accounted for with the 29 bytes.
If the principal is being encoded with candid then it could have up to 34 bytes total (I think).
You were right @LightningLad91, the remaining issue I had was related to the Candid encoding. It adds the ”DIDL” but also serialize values and types (from what I understand from it source code).
When it comes to the Principal I ultimately actually serialized it to a bytes length of 30 (29 bytes + one for the length). See this answer for the reason why and how: Convert principal to Vec 29 bytes length - #3 by levi