Skip to content

Commit 1d53e4c

Browse files
committed
Add insert_token_mut to Value
1 parent ad6a077 commit 1d53e4c

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

plutus-ledger-api/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
This changelog is based on [Keep A
66
Changelog](https://keepachangelog.com/en/1.1.0).
77

8+
## 3.1.0
9+
10+
### Added
11+
12+
- Add `insert_token_mut` to `Value`
13+
14+
## v3.0.4
15+
16+
### Changed
17+
18+
- Updating all dependencies to latest minor versions
19+
820
## v3.0.3
921

1022
### Changed

plutus-ledger-api/src/v1/value.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,26 +263,42 @@ impl Value {
263263
}
264264

265265
/// Create a new value by inserting a new token or replacing the existing quantity.
266-
pub fn insert_token(&self, cs: &CurrencySymbol, tn: &TokenName, a: &BigInt) -> Self {
266+
pub fn insert_token(
267+
&self,
268+
currency_symbol: &CurrencySymbol,
269+
token_name: &TokenName,
270+
amount: &BigInt,
271+
) -> Self {
267272
let mut result_map = self.clone();
268273

269-
result_map.insert_token_mut(cs, tn, a);
274+
result_map.insert_token_mut(currency_symbol.clone(), token_name.clone(), amount.clone());
270275

271276
result_map
272277
}
273278
/// Insert a new token into the value, or replace the existing quantity.
274-
pub fn insert_token_mut(&mut self, cs: &CurrencySymbol, tn: &TokenName, a: &BigInt) {
275-
self.0
276-
.entry(cs.clone())
277-
.and_modify(|tn_map| {
278-
tn_map
279-
.entry(tn.clone())
280-
.and_modify(|old_a| {
281-
old_a.clone_from(a);
282-
})
283-
.or_insert_with(|| a.clone());
284-
})
285-
.or_insert_with(|| singleton((tn.clone(), a.clone())));
279+
pub fn insert_token_mut(
280+
&mut self,
281+
currency_symbol: CurrencySymbol,
282+
token_name: TokenName,
283+
amount: BigInt,
284+
) {
285+
let tn_map = self.0.get_mut(&currency_symbol);
286+
287+
match tn_map {
288+
None => {
289+
self.0
290+
.insert(currency_symbol, singleton((token_name, amount)));
291+
}
292+
Some(tn_map) => {
293+
let val = tn_map.get_mut(&token_name);
294+
match val {
295+
None => {
296+
tn_map.insert(token_name, amount);
297+
}
298+
Some(old_amount) => *old_amount = amount,
299+
}
300+
}
301+
}
286302
}
287303

288304
/// Return true if the value don't have any entries.

0 commit comments

Comments
 (0)