Skip to content

Commit 2ee52e3

Browse files
committed
Refactor jet pointer and environment handling in Rust generation
1 parent d190505 commit 2ee52e3

1 file changed

Lines changed: 21 additions & 40 deletions

File tree

Haskell-Generate/GenRustJets.hs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,28 @@ rustJetSourceTy = rustJetTy "source_ty" (\(SomeArrow jet) -> unreflect (fst (rei
187187
rustJetTargetTy :: Module -> Doc a
188188
rustJetTargetTy = rustJetTy "target_ty" (\(SomeArrow jet) -> unreflect (snd (reifyArrow jet)))
189189

190-
rustJetPtr :: Module -> Doc a
191-
rustJetPtr mod = vsep $
192-
[ nest 4 (vsep ("fn c_jet_ptr(&self) -> &dyn Fn(&mut CFrameItem, CFrameItem, &Self::CJetEnvironment) -> bool {" :
193-
if modname == "Bitcoin"
194-
then ["unimplemented!(\"Bitcoin jets have not yet been implemented.\")"]
195-
else [ nest 4 (vsep ("match self {" :
196-
map (<>comma)
197-
[ pretty modname <> "::" <> pretty (jetName jet) <+> "=>" <+>
198-
pretty ("&simplicity_sys::c_jets::jets_wrapper::"++cJetName jet)
199-
| SomeArrow jet <- moduleJets mod
200-
]))
201-
, "}"
202-
]))
190+
rustJetCJetPtr :: Module -> Doc a
191+
rustJetCJetPtr mod = vsep $
192+
[ nest 4 (vsep
193+
[ pretty ("pub(crate) fn c_jet_ptr(jet: &" ++ modname ++ ") -> fn(&mut CFrameItem, CFrameItem, &" ++ cJetEnvType ++ ") -> bool {")
194+
, if modname == "Bitcoin"
195+
then " unimplemented!(\"Bitcoin jets have not yet been implemented.\")"
196+
else vsep
197+
[ nest 4 (vsep ("match jet {" :
198+
map (<>comma)
199+
[ pretty modname <> "::" <> pretty (jetName jet) <+> "=>" <+>
200+
pretty ("simplicity_sys::c_jets::jets_wrapper::"++cJetName jet)
201+
| SomeArrow jet <- moduleJets mod
202+
]))
203+
, "}"
204+
]
205+
])
203206
, "}"
204207
]
205208
where
206209
modname = rustModuleName mod
210+
cJetEnvType | Just "Elements" == moduleName mod = "CElementsTxEnv" :: String
211+
| otherwise = "()"
207212

208213
rustJetEncode :: Module -> Doc a
209214
rustJetEncode mod =
@@ -250,38 +255,17 @@ rustJetImpl :: Module -> Doc a
250255
rustJetImpl mod = vsep $
251256
[ nest 4 (vsep $ punctuate line
252257
["impl Jet for" <+> pretty modname <+> "{"
253-
, env
254258
, rustJetCmr mod
255259
, rustJetSourceTy mod
256260
, rustJetTargetTy mod
257261
, rustJetEncode mod
258262
, rustJetDecode mod
259-
, rustJetPtr mod
260263
, rustJetCost mod
261264
])
262265
, "}"
263266
]
264267
where
265268
modname = rustModuleName mod
266-
env = vsep
267-
[ pretty $ "type Environment = "++env++";"
268-
, pretty $ "type CJetEnvironment = "++cEnv++";"
269-
, ""
270-
, pretty $ "fn c_jet_env("++envArg++": &Self::Environment) -> &Self::CJetEnvironment {"
271-
, pretty $ " "++envBody
272-
, "}"
273-
]
274-
where
275-
env | Nothing <- moduleName mod = "()"
276-
| Just "Elements" == moduleName mod = "ElementsEnv<std::sync::Arc<elements::Transaction>>"
277-
| Just name <- moduleName mod = name ++ "Env"
278-
cEnv | Just "Elements" == moduleName mod = "CElementsTxEnv"
279-
| otherwise = "()"
280-
envArg | Just "Bitcoin" == moduleName mod = "_env"
281-
| otherwise = "env"
282-
envBody | Nothing == moduleName mod = "env"
283-
| Just "Bitcoin" == moduleName mod = "unimplemented!(\"Unspecified CJetEnvironment for Bitcoin jets\")"
284-
| otherwise = "env.c_tx_env()"
285269

286270
rustJetEnum :: Module -> Doc a
287271
rustJetEnum mod = vsep
@@ -354,12 +338,8 @@ rustImports mod = vsep (map (<> semi)
354338
, "use std::{fmt, str}"
355339
] ++ envImports))
356340
where
357-
envImports | Nothing == moduleName mod = []
358-
| Just "Bitcoin" == moduleName mod = ["use crate::jet::bitcoin::BitcoinEnv"]
359-
| Just name <- moduleName mod =
360-
[ pretty $ "use crate::jet::"++map toLower name++"::"++name++"Env"
361-
, pretty $ "use simplicity_sys::C"++name++"TxEnv"
362-
]
341+
envImports | Just "Elements" == moduleName mod = ["use simplicity_sys::CElementsTxEnv"]
342+
| otherwise = []
363343

364344
rustJetDoc :: Module -> SimpleDocStream a
365345
rustJetDoc mod = layoutPretty layoutOptions $ vsep (map (<> line)
@@ -369,6 +349,7 @@ rustJetDoc mod = layoutPretty layoutOptions $ vsep (map (<> line)
369349
, rustJetImpl mod
370350
, rustJetDisplay mod
371351
, rustJetFromStr mod
352+
, rustJetCJetPtr mod
372353
])
373354

374355
rustFFIImports :: Doc a

0 commit comments

Comments
 (0)