gpt4 book ai didi

Rust cw_storage_plus - how do I satisfy Serialize, DeserializeOwned, and Deserialize<'de> in my struct?(Rust cw_storage_plus -如何在结构体中满足Serialize、SerializeOwned和Serialize <$'de>?)

翻译 作者:bug小助手 更新时间:2023-10-26 21:44:21 30 4
gpt4 key购买 nike



I'm teachning myself Rust by building a CosmWasm contract.

我正在通过建立一份CosmWasm合同来教自己Rust。


Game is defined by

游戏的定义是


use cosmwasm_std::{Addr, Int256};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
pub struct Game {
id: u64,
owner: Addr,
round: Int256, // as close to infinite number of rounds as I can think of
status: GameStatus,
slots: [Slot; 9],
}

(Slot is another struct, and GameStatus is an enum, all prefixed with their own #[derive... macros.)

(Slot是另一个结构,而GameStatus是一个枚举,前缀都是它们自己的#[派生...宏。)


I have defined a Map of Games as follows

我定义了一个游戏地图,如下所示


use cw_storage_plus::{Item, Map};

pub const GAMES: Item<Map<u64, Game>> = Item::new("games");

In my contract's instantiate function I have the following

在我的合同的实例化函数中,我有以下内容


// game is an instance of a Game
GAMES.save(deps.storage, 0, &game)?;

GAMES.save shows error

GAMES.save显示错误


the method `save` exists for struct `Item<'static, Map<'static, u64, Game>>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`cw_storage_plus::Map<'_, u64, Game>: Serialize`
`cw_storage_plus::Map<'_, u64, Game>: DeserializeOwned`
`cw_storage_plus::Map<'_, u64, Game>: Deserialize<'de>`
which is required by `cw_storage_plus::Map<'_, u64, Game>: DeserializeOwned`

From what I can tell the issue is not with the Item or the Map but the Game struct.

据我所知,问题不在于物品或地图,而在于游戏结构。


I've tried importing DeserializeOwned as in

我已尝试导入反序列化Owned,如


use serde::de::{DeserializeOwned}

but there is no derive mechanism for it so I can't just add it to the derive.

但它没有派生机制,所以我不能只把它加到派生中。


I read at stackoverflow.com/a/60091762 that

我在Stackoverflow.com/a/60091762上看到



You just have to add #[derive(serde::Deserialize)] and use the DeserializeOwned.



I feel like I am missing something obvious but I've not been able to work it out.

我觉得我遗漏了一些明显的东西,但我一直无法解决这个问题。


更多回答

Why are you using Item<Map> instead of just Map?

为什么使用Item而不是只使用Map?

following the example in book.cosmwasm.com/cross-contract/map-storage.html - I need an Item because it's how I save the state on-chain.

遵循book.cosmwasm.com/cross-contract/map-storage.html中的示例-我需要一个项,因为它是我在线保存状态的方式。

lol turns out I simply misread the docs. You are spot on. Fixed it. Many thanks.

哈哈,原来我只是看错了文档。你说得太对了。修好了。非常感谢。

"as close to infinite number of rounds as I can think of" u64 is plenty. If you did one round a second, it will take you 42 times the age of the universe to overflow.

“接近无限数量的回合,因为我能想到的”u64是足够的。如果你一秒钟转一圈,你需要42倍于宇宙年龄的时间才能溢出。

优秀答案推荐
更多回答

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com