// two structures representing different storage states
struct NftItemStorage {
itemIndex: uint64
collectionAddress: address
ownerAddress: address
content: cell
}
struct NftItemStorageNotInitialized {
itemIndex: uint64
collectionAddress: address
}
// instead of the usual `load()` method — `startLoading()`
fun NftItemStorage.startLoading() {
return NftItemStorageLoader.fromCell(contract.getData())
}
fun NftItemStorage.save(self) {
contract.setData(self.toCell())
}
// this helper detects shape of a storage
struct NftItemStorageLoader {
itemIndex: uint64
collectionAddress: address
private rest: RemainingBitsAndRefs
}
// when `rest` is empty, `collectionAddress` is the last field
fun NftItemStorageLoader.isNotInitialized(self) {
return self.rest.isEmpty()
}
// `endLoading` continues loading when `rest` is not empty
fun NftItemStorageLoader.endLoading(mutate self): NftItemStorage {
return {
itemIndex: self.itemIndex,
collectionAddress: self.collectionAddress,
ownerAddress: self.rest.loadAny(),
content: self.rest.loadAny(),
}
}