Rust cw_storage_plus - how do I satisfy Serialize, DeserializeOwned, and Deserialize<'de> in my struct?

80 Views Asked by At

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

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.)

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

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

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

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.

0

There are 0 best solutions below