Skip to content

Commit 1d0e945

Browse files
committed
WithExtra types use Cow
1 parent 99cafa5 commit 1d0e945

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

plutus-ledger-api/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Changelog](https://keepachangelog.com/en/1.1.0).
2020
- Addresses are formatted using bech32, if supplemented with network id
2121
- CurrencySymbols are formatted as hex if native tokens or as the `lovelace`
2222
string
23+
- `WithExtraInfo` types are now using Cow under the hood, allowing ownership of
24+
the data
2325

2426
## 3.1.0
2527

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Types related to Cardano addresses
2+
use std::borrow::Cow;
23
use std::str::FromStr;
34

45
use anyhow::anyhow;
@@ -41,7 +42,14 @@ pub struct Address {
4142
impl Address {
4243
pub fn with_extra_info<'a>(&'a self, network_tag: u8) -> AddressWithExtraInfo<'a> {
4344
AddressWithExtraInfo {
44-
address: self,
45+
address: Cow::Borrowed(self),
46+
network_tag,
47+
}
48+
}
49+
50+
pub fn into_address_with_extra_info<'a>(self, network_tag: u8) -> AddressWithExtraInfo<'a> {
51+
AddressWithExtraInfo {
52+
address: Cow::Owned(self),
4553
network_tag,
4654
}
4755
}
@@ -90,7 +98,7 @@ impl TryFromCSL<csl::Address> for Address {
9098
/// Address with network information. The `WithExtraInfo` variant has Display instance, serializing into
9199
/// a bech32 address format.
92100
pub struct AddressWithExtraInfo<'a> {
93-
pub address: &'a Address,
101+
pub address: Cow<'a, Address>,
94102
pub network_tag: u8,
95103
}
96104

@@ -113,6 +121,22 @@ impl TryFromPLA<AddressWithExtraInfo<'_>> for csl::Address {
113121
}
114122
}
115123

124+
impl<'a> TryFromCSL<csl::Address> for AddressWithExtraInfo<'a> {
125+
fn try_from_csl(value: &csl::Address) -> Result<Self, TryFromCSLError>
126+
where
127+
Self: Sized,
128+
{
129+
Ok(AddressWithExtraInfo {
130+
address: Cow::Owned(value.try_to_pla()?),
131+
network_tag: value.network_id().map_err(|err| {
132+
TryFromCSLError::ImpossibleConversion(format!(
133+
"Couldn't extract network tag from address: {err}"
134+
))
135+
})?,
136+
})
137+
}
138+
}
139+
116140
/// Serializing into a bech32 address format.
117141
impl std::fmt::Display for AddressWithExtraInfo<'_> {
118142
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -334,15 +358,15 @@ impl FromCSL<csl::Pointer> for StakingCredential {
334358

335359
#[derive(Clone, Debug)]
336360
pub struct RewardAddressWithExtraInfo<'a> {
337-
pub staking_credential: &'a StakingCredential,
361+
pub staking_credential: Cow<'a, StakingCredential>,
338362
pub network_tag: u8,
339363
}
340364

341365
impl TryFromPLA<RewardAddressWithExtraInfo<'_>> for csl::RewardAddress {
342366
fn try_from_pla(val: &RewardAddressWithExtraInfo<'_>) -> Result<Self, TryFromPLAError> {
343367
Ok(csl::RewardAddress::new(
344368
val.network_tag,
345-
&val.staking_credential.try_to_csl()?,
369+
&val.staking_credential.as_ref().try_to_csl()?,
346370
))
347371
}
348372
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Types related to Plutus Redeemers
22
3+
use std::borrow::Cow;
4+
35
use cardano_serialization_lib as csl;
46

57
#[cfg(feature = "lbf")]
@@ -26,16 +28,16 @@ pub struct Redeemer(pub PlutusData);
2628

2729
#[derive(Clone, Debug)]
2830
pub struct RedeemerWithExtraInfo<'a> {
29-
pub redeemer: &'a Redeemer,
30-
pub tag: &'a csl::RedeemerTag,
31+
pub redeemer: Cow<'a, Redeemer>,
32+
pub tag: csl::RedeemerTag,
3133
pub index: u64,
3234
}
3335

3436
impl TryFromPLA<RedeemerWithExtraInfo<'_>> for csl::Redeemer {
3537
fn try_from_pla<'a>(val: &RedeemerWithExtraInfo<'_>) -> Result<csl::Redeemer, TryFromPLAError> {
36-
let Redeemer(plutus_data) = val.redeemer;
38+
let Redeemer(plutus_data) = val.redeemer.as_ref();
3739
Ok(csl::Redeemer::new(
38-
val.tag,
40+
&val.tag,
3941
&val.index.try_to_csl()?,
4042
&plutus_data.try_to_csl()?,
4143
&csl::ExUnits::new(&csl::BigNum::from(0u64), &csl::BigNum::from(0u64)),

plutus-ledger-api/src/v2/transaction.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Types related to Cardano transactions.
22
3+
use std::borrow::Cow;
34
use std::collections::BTreeMap;
45

56
use cardano_serialization_lib as csl;
@@ -86,17 +87,17 @@ impl TryFromCSL<csl::TransactionOutputs> for Vec<TransactionOutput> {
8687

8788
#[derive(Clone, Debug)]
8889
pub struct TransactionOutputWithExtraInfo<'a> {
89-
pub transaction_output: &'a TransactionOutput,
90-
pub scripts: &'a BTreeMap<ScriptHash, csl::PlutusScript>,
90+
pub transaction_output: Cow<'a, TransactionOutput>,
91+
pub scripts: Cow<'a, BTreeMap<ScriptHash, csl::PlutusScript>>,
9192
pub network_id: u8,
92-
pub data_cost: &'a csl::DataCost,
93+
pub data_cost: Cow<'a, csl::DataCost>,
9394
}
9495

9596
impl TryFromPLA<TransactionOutputWithExtraInfo<'_>> for csl::TransactionOutput {
9697
fn try_from_pla(val: &TransactionOutputWithExtraInfo<'_>) -> Result<Self, TryFromPLAError> {
9798
let mut output_builder = csl::TransactionOutputBuilder::new().with_address(
9899
&AddressWithExtraInfo {
99-
address: &val.transaction_output.address,
100+
address: Cow::Borrowed(&val.transaction_output.address),
100101
network_tag: val.network_id,
101102
}
102103
.try_to_csl()?,
@@ -127,7 +128,7 @@ impl TryFromPLA<TransactionOutputWithExtraInfo<'_>> for csl::TransactionOutput {
127128

128129
let value_without_min_utxo = val.transaction_output.value.try_to_csl()?;
129130

130-
let mut calc = csl::MinOutputAdaCalculator::new_empty(val.data_cost)
131+
let mut calc = csl::MinOutputAdaCalculator::new_empty(val.data_cost.as_ref())
131132
.map_err(TryFromPLAError::CSLJsError)?;
132133
calc.set_amount(&value_without_min_utxo);
133134
match &val.transaction_output.datum {
@@ -205,7 +206,7 @@ pub struct TransactionInfo {
205206

206207
#[derive(Clone, Debug)]
207208
pub struct WithdrawalsWithExtraInfo<'a> {
208-
pub withdrawals: &'a AssocMap<StakingCredential, BigInt>,
209+
pub withdrawals: Cow<'a, AssocMap<StakingCredential, BigInt>>,
209210
pub network_tag: u8,
210211
}
211212

@@ -217,7 +218,7 @@ impl TryFromPLA<WithdrawalsWithExtraInfo<'_>> for csl::Withdrawals {
217218
.try_fold(csl::Withdrawals::new(), |mut acc, (s, q)| {
218219
acc.insert(
219220
&RewardAddressWithExtraInfo {
220-
staking_credential: s,
221+
staking_credential: Cow::Borrowed(s),
221222
network_tag: val.network_tag,
222223
}
223224
.try_to_csl()?,

0 commit comments

Comments
 (0)