Skip to content

Commit 5961690

Browse files
committed
Rust codegen: use to_owned instead of clone
1 parent 7173e0c commit 5961690

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Rust/Print/LamVal.hs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ import LambdaBuffers.ProtoCompat qualified as PC
2121
import Prettyprinter (Doc, Pretty (pretty), align, angles, braces, brackets, colon, comma, dot, dquotes, encloseSep, equals, group, langle, lbracket, line, lparen, parens, pipe, punctuate, rangle, rbracket, rparen, semi, space, vsep, (<+>))
2222
import Proto.Codegen_Fields qualified as P
2323

24-
{- | Clone a value (converting a type to owned)
25-
As in codegen we cannot know whether a type is owned or borrowed, we make sure to have an owned type by making a clone.
26-
We must also borrow it first for the same reason (worst case it's a double && which Rust knows how to deal with)
27-
-}
28-
clone :: Doc ann -> Doc ann
29-
clone = useTraitMethod RR.cloneTrait "clone" . borrow
24+
toOwned :: Doc ann -> Doc ann
25+
toOwned = useTraitMethod RR.toOwnedTrait "to_owned"
3026

3127
borrow :: Doc ann -> Doc ann
3228
borrow doc = "&" <> doc
@@ -290,9 +286,9 @@ printProductE pkgs iTyDefs (qtyN@(mn', tyN'), _) vals = do
290286
return $ ctorDoc <> encloseSep lparen rparen comma (fieldDocs <> phantomFieldDocs)
291287

292288
printMaybeBoxed :: MonadPrint m => R.PkgMap -> PC.TyDefs -> (LV.ValueE, Bool) -> m (Doc ann)
293-
printMaybeBoxed pkgs iTyDefs (val, False) = clone <$> printValueE pkgs iTyDefs val
289+
printMaybeBoxed pkgs iTyDefs (val, False) = toOwned <$> printValueE pkgs iTyDefs val
294290
printMaybeBoxed pkgs iTyDefs (val, True) = do
295-
valDoc <- clone <$> printValueE pkgs iTyDefs val
291+
valDoc <- toOwned <$> printValueE pkgs iTyDefs val
296292
return $ R.printRsQValName RR.boxNew <> parens valDoc
297293

298294
printTupleE :: MonadPrint m => R.PkgMap -> PC.TyDefs -> LV.ValueE -> LV.ValueE -> m (Doc ann)

lambda-buffers-codegen/src/LambdaBuffers/Codegen/Rust/Print/Refs.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module LambdaBuffers.Codegen.Rust.Print.Refs (
1313
eqTrait,
1414
isPlutusDataTrait,
1515
jsonTrait,
16+
toOwnedTrait,
1617
)
1718
where
1819

@@ -59,3 +60,6 @@ isPlutusDataTrait = R.qForeignRef R.MkTraitName "plutus-ledger-api" ["plutus_dat
5960

6061
jsonTrait :: R.QTraitName
6162
jsonTrait = R.qForeignRef R.MkTraitName "lbr-prelude" ["json"] "Json"
63+
64+
toOwnedTrait :: R.QTraitName
65+
toOwnedTrait = R.qForeignRef R.MkTraitName "std" ["borrow"] "ToOwned"

0 commit comments

Comments
 (0)