Repository navigation
Expand file tree
/
Copy pathgenerate.py
More file actions
2737 lines (2523 loc) · 113 KB
/
Copy pathgenerate.py
File metadata and controls
2737 lines (2523 loc) · 113 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Generate the PyO3 bindings for `rust_physics_engine`.
The library is about 266,000 lines across 71 top-level modules, and its
public surface is roughly 4,100 free functions, 2,200 inherent methods,
336 structs, 88 enums and 117 constants. Writing that by hand is not the
hard part -- keeping it in step with the library afterwards is. A binding
written once goes stale on the first commit that adds a function, and the
staleness is invisible: nothing fails, the function simply is not there.
So the bindings are derived from the source instead. `rustscan` reads the
crate's public API; this file decides how each item crosses into Python
and writes the wrapper. Re-running it after a change to the library
produces the binding for the changed library, and CI re-runs it and fails
if the committed output differs, which is the only thing that keeps
generated code honest.
What it cannot bind it says so about, in COVERAGE.md, with the reason --
a generic parameter it cannot monomorphise, a `&dyn Trait` argument with
no Python equivalent. Silence about a gap is worse than the gap.
Usage:
python3 generate.py # write the bindings
python3 generate.py --check # fail if what is committed is stale
"""
from __future__ import annotations
import argparse
import dataclasses
import os
import re
import shutil
import sys
from dataclasses import dataclass, field
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import rustscan # noqa: E402
CRATE_ROOT = os.path.abspath(os.path.join(HERE, "..", ".."))
SRC = os.path.join(CRATE_ROOT, "src")
OUT_RS = os.path.join(HERE, "src", "generated")
OUT_PY = os.path.join(HERE, "python", "numeria")
PKG = "numeria"
# Error types are exceptions, not classes.
ERROR_TYPES = {
"error::SolveError",
"error::GeomError",
"units::quantity::DimError",
"codes::reed_solomon::TooManyErrors",
"graph::paths::NegativeCycle",
}
# Types with an exact Python counterpart, translated rather than wrapped.
IDENTIFIED = {
"fractals::Complex": "complex",
"exact::bigint::BigInt": "bigint",
"exact::rational::Rational": "rational",
}
PRIMS = {
"f64": "float",
"f32": "float",
"usize": "int",
"isize": "int",
"u8": "int",
"u16": "int",
"u32": "int",
"u64": "int",
"i8": "int",
"i16": "int",
"i32": "int",
"i64": "int",
"bool": "bool",
"char": "str",
}
PY_KEYWORDS = {
"False", "None", "True", "and", "as", "assert", "async", "await", "break",
"class", "continue", "def", "del", "elif", "else", "except", "finally",
"for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal",
"not", "or", "pass", "raise", "return", "try", "while", "with", "yield",
"match", "case", "type",
}
RUST_KEYWORDS = {
"as", "break", "const", "continue", "crate", "dyn", "else", "enum",
"extern", "false", "fn", "for", "if", "impl", "in", "let", "loop",
"match", "mod", "move", "mut", "pub", "ref", "return", "self", "static",
"struct", "super", "trait", "true", "type", "unsafe", "use", "where",
"while", "async", "await", "box", "final", "macro", "override", "priv",
"try", "typeof", "unsized", "virtual", "yield", "abstract", "become",
"do",
}
# ── The type language ───────────────────────────────────────────────────
@dataclass(frozen=True)
class Ty:
"""A Rust type, reduced to the shapes that matter for binding.
`by_ref` and `mutable` are kept because they change what the wrapper
has to pass. `Vec3` and `&Vec3` need different call sites, and
`&mut [f64]` is not an input at all -- it is an output written through
an argument, and a binding that quietly dropped the writes would be
worse than one that refused to bind it.
"""
kind: str
inner: tuple = ()
name: str = ""
n: int = 0
by_ref: bool = False
mutable: bool = False
def amp(self, expr: str) -> str:
"""`expr`, borrowed if this type is a reference."""
return f"&{expr}" if self.by_ref else expr
def __repr__(self) -> str: # pragma: no cover - debugging aid
pre = ("&mut " if self.mutable else "&") if self.by_ref else ""
if self.kind in ("prim", "user"):
return f"{pre}{self.kind}:{self.name}"
return f"{pre}{self.kind}({', '.join(map(repr, self.inner))})"
BAD = Ty("bad")
class Resolver:
"""Turns a type as written into a fully-qualified crate path."""
def __init__(self, crate: rustscan.Crate):
self.crate = crate
self.by_path: dict[str, object] = {}
self.by_name: dict[str, list[str]] = {}
for item in list(crate.structs) + list(crate.enums):
self.by_path[item.path] = item
self.by_name.setdefault(item.name, []).append(item.path)
self.aliases = {}
for a in crate.aliases:
self.aliases[f"{a.module}::{a.name}" if a.module else a.name] = a.target
# Every module that exists, so `use` targets can be checked.
self.modules = set()
for item in list(crate.structs) + list(crate.enums) + list(crate.funcs):
parts = item.module.split("::")
for i in range(len(parts)):
self.modules.add("::".join(parts[: i + 1]))
def resolve(self, name: str, file: str, module: str) -> str | None:
"""Full crate path for the type `name` as written inside `file`."""
name = name.strip()
if name.startswith("crate::"):
name = name[len("crate::") :]
if name.startswith("self::"):
name = f"{module}::{name[6:]}"
head = name.split("::")[0]
tail = name.split("::")[1:]
candidates: list[str] = []
# Defined in the same module.
candidates.append(f"{module}::{name}" if module else name)
# Imported by name.
mapped = self.crate.uses.get(file, {}).get(head)
if mapped:
candidates.append("::".join([mapped] + tail))
# Brought in by a glob.
for g in self.crate.glob_uses.get(file, []):
candidates.append(f"{g}::{name}")
# An ancestor module.
parts = module.split("::") if module else []
for i in range(len(parts) - 1, -1, -1):
candidates.append("::".join(parts[:i] + [name]))
# Written out in full already.
candidates.append(name)
# A unique match anywhere in the crate.
if len(self.by_name.get(name, [])) == 1:
candidates.append(self.by_name[name][0])
for c in candidates:
if c in self.by_path:
return c
if c in self.aliases:
return None # an alias; the caller re-parses the target
return None
def alias_target(self, name: str, file: str, module: str) -> str | None:
name = name.strip().removeprefix("crate::")
head = name.split("::")[0]
for cand in (
f"{module}::{name}" if module else name,
self.crate.uses.get(file, {}).get(head, ""),
name,
):
if cand and cand in self.aliases:
return self.aliases[cand]
return None
def parse_type(text: str, res: Resolver, file: str, module: str, depth: int = 0) -> Ty:
"""Parse a Rust type into the reduced language above."""
if depth > 8:
return BAD
s = " ".join(text.split()).strip()
if not s:
return Ty("unit")
if s in ("()", "!"):
return Ty("unit")
# A leading reference belongs to the type it points at, not to a
# wrapper around it: `&[f64]` is the slice type, borrowed.
if s.startswith("&"):
m = re.match(r"^&\s*(?:'[A-Za-z_][A-Za-z0-9_]*\s+)?(mut\s+)?", s)
inner = parse_type(s[m.end() :], res, file, module, depth + 1)
if inner.kind == "bad":
return BAD
return dataclasses.replace(inner, by_ref=True, mutable=bool(m.group(1)))
if s.startswith("*"):
return BAD
# Slices and arrays.
if s.startswith("[") and s.endswith("]"):
body = s[1:-1]
parts = rustscan._split_top(body)
if len(parts) == 1 and ";" in body:
base, count = body.rsplit(";", 1)
count = count.strip()
elem = parse_type(base, res, file, module, depth + 1)
if elem.kind == "bad" or not count.isdigit():
return BAD
return Ty("array", (elem,), n=int(count))
elem = parse_type(body, res, file, module, depth + 1)
return BAD if elem.kind == "bad" else Ty("vec", (elem,))
# Tuples.
if s.startswith("(") and s.endswith(")") and _balanced(s):
parts = rustscan._split_top(s[1:-1])
if len(parts) == 1:
return parse_type(parts[0], res, file, module, depth + 1)
elems = tuple(parse_type(p, res, file, module, depth + 1) for p in parts)
if any(e.kind == "bad" for e in elems):
return BAD
return Ty("tuple", elems)
# Callables. A bare `fn(..)` pointer is deliberately not one: a Python
# callable has no address to hand over.
m = re.match(r"^(?:dyn\s+|impl\s+)(?:Fn|FnMut|FnOnce)\s*\((.*?)\)\s*(?:->\s*(.+?))?(?:\s*\+\s*.*)?$", s)
if m:
argtypes = tuple(
parse_type(p, res, file, module, depth + 1) for p in rustscan._split_top(m.group(1))
)
ret = parse_type(m.group(2) or "()", res, file, module, depth + 1)
if any(a.kind == "bad" for a in argtypes) or ret.kind == "bad":
return BAD
return Ty("callable", argtypes + (ret,))
# `impl Iterator<Item = T>`: a Python caller gets the list. The
# iterators here are all finite by construction -- subsets, dyck
# paths, partitions -- so collecting is a change of laziness, not of
# termination.
m = re.match(r"^impl\s+Iterator\s*<\s*Item\s*=\s*(.+?)\s*>(?:\s*\+.*)?$", s)
if m:
elem = parse_type(m.group(1), res, file, module, depth + 1)
return BAD if elem.kind == "bad" else Ty("iter", (elem,))
if s.startswith("dyn ") or s.startswith("impl "):
return BAD
if s in ("String", "str", "std::string::String"):
return Ty("str")
if s in PRIMS:
return Ty("prim", name=s)
# Generic containers.
m = re.match(r"^([A-Za-z_][A-Za-z0-9_:]*)\s*<(.+)>$", s)
if m:
head = m.group(1).split("::")[-1]
args = [parse_type(p, res, file, module, depth + 1) for p in rustscan._split_top(m.group(2))]
if head in ("Vec", "VecDeque"):
return BAD if args[0].kind == "bad" else Ty("vec", (args[0],))
if head == "Box":
return args[0] if args else BAD
if head == "Option":
return BAD if args[0].kind == "bad" else Ty("opt", (args[0],))
if head == "Result":
ok = args[0]
parts = rustscan._split_top(m.group(2))
errname = parts[1].strip() if len(parts) > 1 else ""
if ok.kind == "bad":
return BAD
return Ty("result", (ok,), name=errname.split("::")[-1])
return BAD
# A plain path: a struct, an enum, or an alias for one.
if re.match(r"^[A-Za-z_][A-Za-z0-9_:]*$", s):
if s.split("::")[-1] == "Self":
return Ty("selfty")
target = res.alias_target(s, file, module)
if target is not None:
return parse_type(target, res, file, module, depth + 1)
full = res.resolve(s, file, module)
if full:
return Ty("user", name=full)
return BAD
def _balanced(s: str) -> bool:
depth = 0
for i, c in enumerate(s):
if c == "(":
depth += 1
elif c == ")":
depth -= 1
if depth == 0 and i != len(s) - 1:
return False
return depth == 0
# ── Wrappers ────────────────────────────────────────────────────────────
@dataclass
class Wrapper:
"""A Python class standing in for one Rust struct or enum."""
item: object
ident: str # the Rust identifier of the wrapper struct
py_name: str
py_module: str
rust_path: str
is_enum: bool
simple_enum: bool
clone: bool
debug: bool
partial_eq: bool
copy: bool
coerce_n: int = 0 # >0: accepts a sequence of this many floats
coerce_rows: bool = False # accepts a sequence of rows
coerce_seq: bool = False # accepts a sequence of floats of any length
unsendable: bool = False
fields: list = field(default_factory=list) # bindable (name, Ty, rust_name)
consts: list = field(default_factory=list)
@property
def arg_ident(self) -> str:
return f"{self.ident}Arg"
def camel(s: str) -> str:
return "".join(p[:1].upper() + p[1:] for p in re.split(r"[_:]+", s) if p)
class Generator:
def __init__(self) -> None:
self.crate = rustscan.scan_crate(SRC)
_add_macro_items(self.crate)
self.res = Resolver(self.crate)
self.wrappers: dict[str, Wrapper] = {}
self.skipped: list[tuple[str, str, str]] = [] # (module, item, reason)
self.counts: dict[str, dict[str, int]] = {}
self.modules: list[str] = []
self._plan_wrappers()
# ── planning ────────────────────────────────────────────────────
def _plan_wrappers(self) -> None:
items = [(s, False) for s in self.crate.structs] + [(e, True) for e in self.crate.enums]
by_name: dict[str, list] = {}
keep = []
for item, is_enum in items:
if item.path in ERROR_TYPES or item.path in IDENTIFIED:
continue
if item.generics.strip():
self.skipped.append((item.module, item.name, "generic type"))
continue
if item.name.startswith("_"):
continue
keep.append((item, is_enum))
by_name.setdefault(item.name, []).append(item)
for item, is_enum in keep:
derives = item.derives()
if len(by_name[item.name]) == 1:
ident = "Py" + item.name
else:
ident = "Py" + camel(item.module.split("::")[-1]) + item.name
if sum(1 for o in by_name[item.name] if o.module.split("::")[-1] == item.module.split("::")[-1]) > 1:
ident = "Py" + camel(item.module) + item.name
simple_enum = is_enum and all(not v.payload for v in item.variants)
unsendable = any(
re.search(r"\bdyn\b|\bRc<|\bCell<|\bRefCell<", f.ty) for f in getattr(item, "fields", [])
)
self.wrappers[item.path] = Wrapper(
item=item,
ident=ident,
py_name=item.name,
py_module=f"{PKG}.{item.module.replace('::', '.')}",
rust_path=f"rust_physics_engine::{item.path}",
is_enum=is_enum,
simple_enum=simple_enum,
clone="Clone" in derives,
debug="Debug" in derives,
partial_eq="PartialEq" in derives,
copy="Copy" in derives,
unsendable=unsendable,
)
# Which structs can be built from a bare sequence?
for path, w in self.wrappers.items():
if w.is_enum or not w.clone:
continue
fields = w.item.fields
if w.item.kind == "named" and fields and all(f.public and f.ty == "f64" for f in fields):
if len(fields) <= 6:
w.coerce_n = len(fields)
if w.item.kind == "named" and len(fields) == 1 and fields[0].ty == "Vec<f64>" and fields[0].public:
w.coerce_seq = True
matrix = self.wrappers.get("linalg::matrix::Matrix")
if matrix:
matrix.coerce_rows = True
# A type is unsendable if it holds something unsendable, however
# deep. PyO3 needs that to be right: a `#[pyclass]` that is not
# `Send` and not declared `unsendable` will not compile, and one
# that is declared `unsendable` must not have the GIL released
# around it. Iterate to a fixed point rather than looking only one
# field deep.
changed = True
while changed:
changed = False
for w in self.wrappers.values():
if w.unsendable or w.is_enum:
continue
for f in w.item.fields:
ty = parse_type(f.ty, self.res, w.item.file, w.item.module)
if self._holds_unsendable(ty):
w.unsendable = True
changed = True
break
# Bindable fields, for getters.
for path, w in self.wrappers.items():
if w.is_enum or w.item.kind != "named":
continue
for f in w.item.fields:
if not f.public:
continue
ty = parse_type(f.ty, self.res, w.item.file, w.item.module)
if ty.kind != "bad" and self.ret_plan(ty, "x") is not None:
w.fields.append((f.name, ty))
# Associated constants.
for c in self.crate.consts:
if not c.owner:
continue
for path, w in self.wrappers.items():
if w.item.name == c.owner and w.item.module == c.module:
ty = parse_type(c.ty, self.res, c.file, c.module)
if self.ret_plan(ty, "x") is not None:
w.consts.append((c.name, ty, c.doc))
break
def _holds_unsendable(self, ty: Ty, depth: int = 0) -> bool:
if depth > 6:
return False
if ty.kind == "user":
w = self.wrappers.get(ty.name)
return bool(w and w.unsendable)
return any(self._holds_unsendable(t, depth + 1) for t in ty.inner)
def is_clone(self, ty: Ty, depth: int = 0) -> bool:
"""Whether an owned value of `ty` can be cloned out of a field."""
if depth > 6:
return False
k = ty.kind
if k in ("prim", "str", "unit"):
return True
if k in ("vec", "array", "opt"):
return self.is_clone(ty.inner[0], depth + 1)
if k == "tuple":
return all(self.is_clone(t, depth + 1) for t in ty.inner)
if k == "user":
if ty.name in IDENTIFIED:
return True
w = self.wrappers.get(ty.name)
return bool(w and (w.clone or w.simple_enum))
return False
# ── argument plans ──────────────────────────────────────────────
#
# Every argument is described by two things: the type the wrapper
# declares, and a Rust expression turning a value of that type into
# what the library wants. Keeping the conversion an *expression*
# rather than a statement is what lets it nest -- the conversion for
# `&[(Vec2, f64)]` is the conversion for `(Vec2, f64)` inside a
# `.map()`, and that in turn is the conversion for `Vec2` and for
# `f64`. Two shapes cannot be expressions and get their own paths:
# callables, which need an object that outlives the call, and `&mut`
# slices, which are outputs and have to be written back afterwards.
def param_type(self, ty: Ty) -> str | None:
"""The type the generated wrapper declares for `ty`."""
k = ty.kind
if k == "prim":
return ty.name
if k == "str":
return "String"
if k == "user":
return self._user_param(ty.name)
if k == "vec":
inner = self.param_type(ty.inner[0])
return None if inner is None else f"Vec<{inner}>"
if k == "array":
inner = self.param_type(ty.inner[0])
return None if inner is None else f"Vec<{inner}>"
if k == "tuple":
parts = [self.param_type(t) for t in ty.inner]
if any(p is None for p in parts):
return None
return "(" + ", ".join(parts) + ")"
if k == "opt":
inner = self.param_type(ty.inner[0])
return None if inner is None else f"Option<{inner}>"
return None
def _user_param(self, path: str) -> str | None:
if path in IDENTIFIED:
return {
"complex": "crate::runtime::coerce::ComplexArg",
"bigint": "crate::runtime::coerce::BigIntArg",
"rational": "crate::runtime::coerce::RationalArg",
}[IDENTIFIED[path]]
w = self.wrappers.get(path)
if w is None or not (w.clone or w.simple_enum):
return None
if w.coerce_n or w.coerce_rows or w.coerce_seq:
return f"crate::generated::types::{w.arg_ident}"
return f"crate::generated::types::{w.ident}"
def conv_expr(self, ty: Ty, var: str) -> tuple[str, bool] | None:
"""Rust expression turning `var` into an owned value of `ty`.
The flag says whether the expression uses `?`, which decides
whether a surrounding `.map()` has to collect into a `PyResult`.
"""
k = ty.kind
if k in ("prim", "str"):
return var, False
if k == "user":
path = ty.name
if path in IDENTIFIED:
return f"{var}.0", False
w = self.wrappers.get(path)
if w is None:
return None
if w.simple_enum:
return f"{var}.to_rust()", False
if w.coerce_n or w.coerce_rows or w.coerce_seq:
return f"{var}.0", False
if not w.clone:
return None
return f"{var}.inner", False
if k == "vec":
inner = self.conv_expr(ty.inner[0], "__e")
if inner is None:
return None
expr, fallible = inner
if expr == "__e":
return var, False
if fallible:
elem = self.rust_type(ty.inner[0])
return (
f"{var}.into_iter().map(|__e| -> PyResult<{elem}> {{ Ok({expr}) }})"
f".collect::<PyResult<Vec<_>>>()?",
True,
)
return f"{var}.into_iter().map(|__e| {expr}).collect::<Vec<_>>()", False
if k == "array":
elem = self.rust_type(ty.inner[0])
if elem is None:
return None
inner = self.conv_expr(ty.inner[0], "__e")
if inner is None:
return None
expr, fallible = inner
body = var if expr == "__e" else (
f"{var}.into_iter().map(|__e| {expr}).collect::<Vec<_>>()"
)
if fallible:
return None
return (
f"<[{elem}; {ty.n}]>::try_from({body}).map_err(|__v: Vec<{elem}>| "
f'pyo3::exceptions::PyValueError::new_err(format!("expected {ty.n} values, got {{}}", __v.len())))?',
True,
)
if k == "tuple":
parts = [self.conv_expr(t, f"{var}.{i}") for i, t in enumerate(ty.inner)]
if any(p is None for p in parts):
return None
fallible = any(f for _e, f in parts)
return "(" + ", ".join(e for e, _f in parts) + ")", fallible
if k == "opt":
inner = self.conv_expr(ty.inner[0], "__o")
if inner is None:
return None
expr, fallible = inner
if expr == "__o":
return var, False
if fallible:
elem = self.rust_type(ty.inner[0])
return (
f"match {var} {{ Some(__o) => Some({expr}), None => None }}",
True,
)
return f"{var}.map(|__o| {expr})", False
return None
def rust_type(self, ty: Ty) -> str | None:
"""The library-side Rust type an owned conversion produces."""
k = ty.kind
if k == "prim":
return ty.name
if k == "str":
return "String"
if k == "user":
if ty.name in IDENTIFIED:
return {
"complex": "rust_physics_engine::fractals::Complex",
"bigint": "rust_physics_engine::exact::bigint::BigInt",
"rational": "rust_physics_engine::exact::rational::Rational",
}[IDENTIFIED[ty.name]]
w = self.wrappers.get(ty.name)
return w.rust_path if w else None
if k == "vec":
inner = self.rust_type(ty.inner[0])
return None if inner is None else f"Vec<{inner}>"
if k == "array":
inner = self.rust_type(ty.inner[0])
return None if inner is None else f"[{inner}; {ty.n}]"
if k == "tuple":
parts = [self.rust_type(t) for t in ty.inner]
if any(p is None for p in parts):
return None
return "(" + ", ".join(parts) + ")"
if k == "opt":
inner = self.rust_type(ty.inner[0])
return None if inner is None else f"Option<{inner}>"
return None
def arg_plan(self, ty: Ty, name: str) -> dict | None:
"""How to accept `ty` from Python.
Returns `param` (the wrapper's parameter type), `pre` (statements
run before the call), `expr` (what is passed to the Rust routine),
`post` (statements run after it), `py` (the stub annotation) and
`owns` (whether the value is free of Python state, and so safe to
hold with the GIL released).
"""
if ty.kind == "callable":
return self._callable_arg(ty, name)
if ty.mutable:
return self._mut_arg(ty, name)
if ty.kind in ("unit", "bad", "selfty", "result", "iter"):
return None
if ty.kind == "user" and ty.by_ref and ty.name in self.wrappers:
w = self.wrappers[ty.name]
if not (w.clone or w.simple_enum):
# No `Clone`, so there is no owned value to make. The
# wrapper is borrowed for the duration of the call
# instead, which is what `&T` means anyway.
return dict(
param=f"pyo3::PyRef<'_, crate::generated::types::{w.ident}>",
pre=[],
expr=f"&{name}.inner",
post=[],
py=w.py_name,
owns=False,
)
param = self.param_type(ty)
conv = self.conv_expr(ty, name)
if param is None or conv is None:
return None
expr, _fallible = conv
pre = [] if expr == name else [f"let {name} = {expr};"]
# An `&[T]` parameter takes a borrow of the owned vector; a `Vec<T>`
# parameter takes it whole. Slices of references need one more
# step, because `&Vec<Vec<f64>>` is not `&[&[f64]]`.
call = self._borrow(ty, name, pre)
if call is None:
return None
return dict(
param=param,
pre=pre,
expr=call,
post=[],
py=self._py_of(ty),
owns=True,
)
def borrow_rust_type(self, ty: Ty) -> str | None:
"""The Rust type as the callee writes it, references and all."""
base = self.rust_type(dataclasses.replace(ty, by_ref=False, mutable=False))
if ty.kind == "vec":
elem = self.borrow_rust_type(ty.inner[0])
if elem is None:
return None
base = f"[{elem}]" if ty.by_ref else f"Vec<{elem}>"
elif ty.kind == "tuple":
parts = [self.borrow_rust_type(t) for t in ty.inner]
if any(p is None for p in parts):
return None
base = "(" + ", ".join(parts) + ")"
elif ty.kind == "opt":
inner = self.borrow_rust_type(ty.inner[0])
if inner is None:
return None
base = f"Option<{inner}>"
elif ty.kind == "str" and ty.by_ref:
return "&str"
if base is None:
return None
return f"&{base}" if ty.by_ref else base
def _needs_deep_borrow(self, ty: Ty, top: bool = True) -> bool:
"""Whether anything below the outermost level is a reference.
`&[f64]` does not need one: a `&Vec<f64>` coerces. `&[&str]` and
`&[(&str, Dim)]` do, because no coercion turns an owned collection
into a collection of borrows.
"""
if not top and ty.by_ref:
return True
return any(self._needs_deep_borrow(t, False) for t in ty.inner)
def _place_expr(self, ty: Ty, place: str) -> str | None:
"""The borrowed form of `ty`, given `place` names the owned value."""
if ty.by_ref:
if ty.kind == "str":
return f"{place}.as_str()"
if ty.kind == "vec":
return f"{place}.as_slice()"
return f"&{place}"
if ty.kind == "prim":
return place
if ty.kind == "tuple":
parts = [self._place_expr(t, f"{place}.{i}") for i, t in enumerate(ty.inner)]
return None if any(p is None for p in parts) else "(" + ", ".join(parts) + ")"
if self.is_clone(ty):
return f"{place}.clone()"
return None
def _borrow(self, ty: Ty, name: str, pre: list[str]) -> str | None:
"""What to write at the call site, given `name` holds the owned value."""
if ty.kind == "vec" and self._needs_deep_borrow(ty.inner[0], top=False):
elem_ty = self.borrow_rust_type(ty.inner[0])
elem_expr = self._place_expr(ty.inner[0], "(*__b)")
if elem_ty is None or elem_expr is None:
return None
pre.append(
f"let {name}__b: Vec<{elem_ty}> = {name}.iter().map(|__b| {elem_expr}).collect();"
)
return f"&{name}__b" if ty.by_ref else f"{name}__b"
if ty.kind == "tuple" and any(
self._needs_deep_borrow(t, top=False) or t.by_ref for t in ty.inner
):
parts = [self._place_expr(t, f"{name}.{i}") for i, t in enumerate(ty.inner)]
if any(p is None for p in parts):
return None
inner = "(" + ", ".join(parts) + ")"
return f"&{inner}" if ty.by_ref else inner
if ty.kind == "opt" and ty.inner[0].by_ref:
inner = ty.inner[0]
if inner.kind == "vec":
take = "__o.as_slice()"
elif inner.kind == "str":
take = "__o.as_str()"
else:
take = "__o"
return f"{name}.as_ref().map(|__o| {take})"
if ty.kind == "array" and (
ty.inner[0].by_ref or self._needs_deep_borrow(ty.inner[0], top=False)
):
return None
return ty.amp(name)
MUT_WRITEBACK = {"f64", "f32", "usize", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64", "bool"}
def _mut_arg(self, ty: Ty, name: str) -> dict | None:
"""`&mut` arguments: an output written through the argument."""
if ty.kind == "user":
w = self.wrappers.get(ty.name)
if w is None or w.simple_enum:
return None
return dict(
param=f"pyo3::PyRefMut<'_, crate::generated::types::{w.ident}>",
pre=[f"let mut {name} = {name};"],
expr=f"&mut {name}.inner",
post=[],
py=w.py_name,
owns=False,
)
if ty.kind == "vec" and ty.inner[0].kind == "user":
elem = ty.inner[0]
param = self.param_type(elem)
conv = self.conv_expr(elem, "__e")
back = self._writeback_object(elem, "__e")
if param is None or conv is None or back is None or conv[1]:
return None
rust_elem = self.rust_type(elem)
return dict(
param="pyo3::Bound<'py, pyo3::PyAny>",
pre=[
f"let mut {name}__v: Vec<{rust_elem}> = {name}.extract::<Vec<{param}>>()?"
f".into_iter().map(|__e| {conv[0]}).collect();",
],
expr=f"&mut {name}__v",
post=[
f"crate::runtime::coerce::write_back_objects(&{name}, "
f"{name}__v.into_iter().map(|__e| {back}).collect::<Vec<_>>())?;"
],
py=f"MutableSequence[{self._py_of(elem)}]",
owns=False,
lifetime=True,
)
if ty.kind == "vec" and ty.inner[0].kind == "prim" and ty.inner[0].name in self.MUT_WRITEBACK:
p = ty.inner[0].name
return dict(
param="pyo3::Bound<'py, pyo3::PyAny>",
pre=[
f"let mut {name}__v: Vec<{p}> = {name}.extract()?;",
],
expr=f"&mut {name}__v",
post=[f"crate::runtime::coerce::write_back(&{name}, &{name}__v)?;"],
py="MutableSequence[" + PRIMS[p] + "]",
owns=False,
lifetime=True,
)
return None
def _writeback_object(self, ty: Ty, var: str) -> str | None:
"""An expression turning an owned Rust value back into a Python one."""
if ty.kind != "user":
return None
if ty.name in IDENTIFIED:
return f"crate::runtime::coerce::Cx({var})" if IDENTIFIED[ty.name] == "complex" else None
w = self.wrappers.get(ty.name)
if w is None or w.simple_enum:
return None
return f"crate::generated::types::{w.ident} {{ inner: {var} }}"
CALLABLE_FALLBACK = {
"f64": "f64::NAN",
"f32": "f32::NAN",
"bool": "false",
"usize": "0",
"u8": "0",
"u16": "0",
"u32": "0",
"u64": "0",
"i8": "0",
"i16": "0",
"i32": "0",
"i64": "0",
}
def _callable_ret(self, ty: Ty) -> tuple[str, str, str] | None:
"""(extracted type, fallback expression, expression turning it into the Rust type)."""
if ty.kind == "prim":
fb = self.CALLABLE_FALLBACK.get(ty.name)
return (ty.name, fb, "__r") if fb else None
if ty.kind == "unit":
return "()", "()", "__r"
if ty.kind == "str":
return "String", "String::new()", "__r"
if ty.kind == "vec" and ty.inner[0].kind == "prim":
return f"Vec<{ty.inner[0].name}>", "Vec::new()", "__r"
if ty.kind == "tuple" and all(t.kind == "prim" for t in ty.inner):
rust = "(" + ", ".join(t.name for t in ty.inner) + ")"
fb = "(" + ", ".join(self.CALLABLE_FALLBACK.get(t.name, "0") for t in ty.inner) + ")"
return rust, fb, "__r"
if ty.kind == "opt":
inner = self._callable_ret(ty.inner[0])
if inner is None or inner[2] != "__r":
return None
return f"Option<{inner[0]}>", "None", "__r"
if ty.kind == "user":
if ty.name in IDENTIFIED and IDENTIFIED[ty.name] == "complex":
return (
"crate::runtime::coerce::ComplexArg",
"crate::runtime::coerce::ComplexArg(rust_physics_engine::fractals::Complex::new(f64::NAN, f64::NAN))",
"__r.0",
)
w = self.wrappers.get(ty.name)
if w is None or not w.clone:
return None
if w.coerce_n:
nan = ", ".join(f"{f.name}: f64::NAN" for f in w.item.fields)
return (
f"crate::generated::types::{w.arg_ident}",
f"crate::generated::types::{w.arg_ident}({w.rust_path} {{ {nan} }})",
"__r.0",
)
if w.coerce_seq:
fname = w.item.fields[0].name
return (
f"crate::generated::types::{w.arg_ident}",
f"crate::generated::types::{w.arg_ident}({w.rust_path} {{ {fname}: Vec::new() }})",
"__r.0",
)
return None
def _callable_arg(self, ty: Ty, name: str) -> dict | None:
*argtys, retty = ty.inner
params, passed = [], []
for i, a in enumerate(argtys):
v = f"__a{i}"
if a.kind == "prim":
params.append(f"{v}: {a.name}")
passed.append(v)
elif a.kind == "str":
params.append(f"{v}: &str")
passed.append(f"{v}.to_string()")
elif a.kind == "vec" and a.inner[0].kind == "prim":
params.append(f"{v}: &[{a.inner[0].name}]")
passed.append(f"{v}.to_vec()")
elif a.kind == "user" and a.name in IDENTIFIED and IDENTIFIED[a.name] == "complex":
params.append(f"{v}: rust_physics_engine::fractals::Complex")
passed.append(f"crate::runtime::coerce::Cx({v})")
elif a.kind == "user" and a.name in self.wrappers and self.wrappers[a.name].clone:
w = self.wrappers[a.name]
ref = "&" if a.by_ref else ""
params.append(f"{v}: {ref}{w.rust_path}")
passed.append(
f"crate::generated::types::{w.ident} {{ inner: {v}{'.clone()' if a.by_ref else ''} }}"
)
else:
return None
ret = self._callable_ret(retty)
if ret is None:
return None
extracted, fallback, unwrap = ret
rust_ret = self.rust_type(retty) if retty.kind != "unit" else "()"
if rust_ret is None:
return None
args_tuple = f"({', '.join(passed)},)" if len(passed) == 1 else f"({', '.join(passed)})"
body = f"__cb_{name}.call::<_, {extracted}>({args_tuple}, {fallback})"
if unwrap != "__r":
body = f"{{ let __r = {body}; {unwrap} }}"
# The closure owns a handle rather than borrowing one, because
# some of these arguments carry a `+ 'static` bound and a borrow
# of a local cannot satisfy it. The wrapper keeps its own handle
# so that it can still ask, after the call, whether the callable
# raised.
body = body.replace(f"__cb_{name}", "__cb")
pre = [
f"let __cb_{name} = std::rc::Rc::new(crate::runtime::Callback::new({name}));",
f"let {name} = {{ let __cb = __cb_{name}.clone(); "
f"move |{', '.join(params)}| -> {rust_ret} {{ {body} }} }};",
]
py_sig = (
"Callable[[" + ", ".join(self._py_of(a) for a in argtys) + "], " + self._py_of(retty) + "]"
)
return dict(
param="pyo3::Py<pyo3::PyAny>",
pre=pre,
expr=ty.amp(name),
post=[],
py=py_sig,
owns=False,
callback=f"__cb_{name}",
)
def _coerce_py(self, w: Wrapper) -> str:
if w.coerce_rows:
return f"{w.py_name} | Sequence[Sequence[float]]"
return f"{w.py_name} | Sequence[float]"
def _py_of(self, ty: Ty) -> str:
if ty.kind == "prim":
return PRIMS[ty.name]
if ty.kind == "str":
return "str"
if ty.kind == "unit":
return "None"
if ty.kind in ("vec", "array", "iter"):
return f"list[{self._py_of(ty.inner[0])}]"
if ty.kind == "tuple":
return "tuple[" + ", ".join(self._py_of(t) for t in ty.inner) + "]"
if ty.kind == "opt":
return f"Optional[{self._py_of(ty.inner[0])}]"
if ty.kind == "result":
return self._py_of(ty.inner[0])
if ty.kind == "callable":
*a, r = ty.inner
return "Callable[[" + ", ".join(self._py_of(x) for x in a) + "], " + self._py_of(r) + "]"
if ty.kind == "user":