Skip to content

Commit 450a451

Browse files
fix the with and with_mut in maps
Signed-off-by: Valentyn Faychuk <valy@faychuk.com>
1 parent e8d3e97 commit 450a451

6 files changed

Lines changed: 232 additions & 68 deletions

File tree

contract_samples/rust/examples/coin.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![no_std]
22
#![no_main]
3+
34
extern crate alloc;
5+
use alloc::vec::Vec;
6+
use alloc::string::String;
47
use amadeus_sdk::*;
5-
use alloc::{vec::Vec, string::String};
68

79
fn vault_key(symbol: &Vec<u8>) -> Vec<u8> {
810
b!("vault:", account_caller(), ":", symbol)

contract_samples/rust/examples/counter.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#![no_std]
22
#![no_main]
3+
34
extern crate alloc;
5+
use alloc::vec::Vec;
6+
use alloc::string::String;
47
use amadeus_sdk::*;
5-
use alloc::{vec::Vec, string::String};
68

79
#[no_mangle]
810
pub extern "C" fn init() {
9-
log("Init called during deployment of contract");
1011
kv_put("inited", "true");
1112
}
1213

contract_samples/rust/examples/deposit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![no_std]
22
#![no_main]
3+
34
extern crate alloc;
5+
use alloc::vec::Vec;
6+
use alloc::string::String;
47
use amadeus_sdk::*;
5-
use alloc::{vec::Vec, string::String};
68

79
fn vault_key(symbol: &Vec<u8>) -> Vec<u8> {
810
b!("vault:", account_caller(), ":", symbol)

contract_samples/rust/examples/nft.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![no_std]
22
#![no_main]
3+
34
extern crate alloc;
5+
use alloc::vec::Vec;
6+
use alloc::string::String;
47
use amadeus_sdk::*;
5-
use alloc::{vec::Vec, string::String};
68

79
#[no_mangle]
810
pub extern "C" fn init() {

contract_samples/rust/examples/showcase.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,46 +36,48 @@ impl Leaderboard {
3636
*self.total_matches += 1;
3737
}
3838

39-
pub fn get_total_matches(&mut self) -> Vec<u8> {
39+
pub fn get_total_matches(&self) -> Vec<u8> {
4040
(*self.total_matches).to_string().into_bytes()
4141
}
4242

4343
pub fn record_match(&mut self, player: String, match_id: u64, score: u16, opponent: String) {
44-
self.players.with_mut(player, |matches| {
45-
matches.with_mut(match_id, |m| {
44+
if let Some(matches) = self.players.get_mut(player.clone()) {
45+
if let Some(m) = matches.get_mut(match_id) {
4646
*m.score = score;
4747
*m.opponent = opponent;
48-
});
49-
});
48+
}
49+
}
5050
*self.total_matches += 1;
5151
}
5252

53-
pub fn get_match_score(&mut self, player: String, match_id: u64) -> Vec<u8> {
54-
self.players.with_mut(player, |matches| {
55-
matches.with_mut(match_id, |m| {
56-
(*m.score).to_string().into_bytes()
57-
})
58-
})
53+
pub fn get_match_score(&self, player: String, match_id: u64) -> Vec<u8> {
54+
if let Some(matches) = self.players.get(player) {
55+
if let Some(m) = matches.get(match_id) {
56+
return (*m.score).to_string().into_bytes();
57+
}
58+
}
59+
"0".to_string().into_bytes()
5960
}
6061

61-
pub fn get_match_opponent(&mut self, player: String, match_id: u64) -> String {
62-
self.players.with_mut(player, |matches| {
63-
matches.with_mut(match_id, |m| {
64-
(*m.opponent).clone()
65-
})
66-
})
62+
pub fn get_match_opponent(&self, player: String, match_id: u64) -> String {
63+
if let Some(matches) = self.players.get(player) {
64+
if let Some(m) = matches.get(match_id) {
65+
return (*m.opponent).clone();
66+
}
67+
}
68+
String::new()
6769
}
6870

6971
pub fn set_tournament_info(&mut self, name: String, prize_pool: u64) {
7072
*self.tournament.name = name;
7173
*self.tournament.prize_pool = prize_pool;
7274
}
7375

74-
pub fn get_tournament_name(&mut self) -> String {
76+
pub fn get_tournament_name(&self) -> String {
7577
(*self.tournament.name).clone()
7678
}
7779

78-
pub fn get_tournament_prize(&mut self) -> Vec<u8> {
80+
pub fn get_tournament_prize(&self) -> Vec<u8> {
7981
(*self.tournament.prize_pool).to_string().into_bytes()
8082
}
8183

@@ -87,7 +89,7 @@ impl Leaderboard {
8789
}
8890
}
8991

90-
pub fn get_player_wins(&mut self, player: String) -> Vec<u8> {
92+
pub fn get_player_wins(&self, player: String) -> Vec<u8> {
9193
if let Some(wins) = self.player_wins.get(&player) {
9294
(*wins).to_string().into_bytes()
9395
} else {

0 commit comments

Comments
 (0)