I want to create custom data structure. I'm a newbie with Rust and Ink! and can't get how to implement my data structure to use it in Mapping
.
I have such a structure:
pub struct TokenInfo {
data: [u8; 284],
in_collection: bool,
init_price: Balance,
init_owner: AccountId,
for_sale: bool,
discount_duration: u64,
birth_date: Timestamp,
}
- Should I implement it as a structure and add a
Mapping
field of that structure to my storage or implement it as separated values and add aMapping
field to each of it to my storage? - I found something close to my problem on this site https://ink.substrate.io/datastructures/custom-datastructure but there's only example with one
bool
variable.
- How to implement
SpreadLayout
for my structure with a bunch of different variables? - How to calculate
FOOTPRINT
? Guess I should add 1 for everyi32
. But what's aboutu8
? Divide by 4? What's about bool? In example above there's added 1 only for onebool
variable. I understand that you can't lock half of a storage cell but don't understand how should I calculate theFOOTPRINT
. I'm really confused about it. I got 82 forFOOTPRINT
, does my calculations right? - Also I don't understand how to implement functions:
pull_spread()
,push_spread()
andclear_spread()
. How to handle a lot of variables in these functions?
- How to use structure implemented as a
SpreadLayout
? I have an error ofunsatisfied trait bounds
when trying to get value of the structure withget()
function ofMapping
or when trying to set value withinsert()
function. Should I use new functions:pull_spread()
,push_spread()
andclear_spread()
instead?
Mapping
with the entire struct means that you'll r/w every field whenever you want to w/r just a single field, which means wasteful storage io and expensive fees for simple contract calls.SpreadAllocate
trait on your root storage struct and usinginitialize_contract
? Both of these are necessary when working withMappings
. As for theSpreadLayout
trait, you shouldn't need to implement it manually since you're working with primitive types - so if you do need to implement such traits (which I don't think you do in this case) you can simply derive them.Mapping
without any problem.SpreadAllocate
for secondary storage and want to have aMapping
toTokenInfo
structure in my root storage.