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
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倍于宇宙年龄的时间才能溢出。
我是一名优秀的程序员,十分优秀!