-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up python bindings generator, and add support for sequences.
This is a significant refactor of the python bindings generator, with the aim of making the generated code a bit cleaner and of better hiding many of our implementation details from the public API. To help prove out the approach, it adds previously-missing support for sequence types. The key idea here is that, for any given `ComponentInterface`, we can finitely enumerate all the possible types used in that interface (including recursive types like sequences) and can give each such type a unique name. This lets us define per-type helper methods on the internal helper objects like `RustBuffer`, rather than putting these as "hidden" methods on the public classes. For example, for each type that lowers into a `RustBuffer`, there is a corresponding `RustBuffer.allocFrom{{ type_name }}` classmethod for lowering it and a `RustBuffer.consumeInto{{ type_name }}` for lifting it. If you squint, this is a little bit like defining private traits and implementing them for each type, except within the constraints of python's much looser type system.
- Loading branch information
Showing
11 changed files
with
604 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from rondpoint import * | ||
|
||
dico = Dictionnaire(Enumeration.DEUX, True, 0, 123456789) | ||
copyDico = copie_dictionnaire(dico) | ||
assert dico == copyDico | ||
|
||
assert copie_enumeration(Enumeration.DEUX) == Enumeration.DEUX | ||
assert copie_enumerations([Enumeration.UN, Enumeration.DEUX]) == [Enumeration.UN, Enumeration.DEUX] | ||
|
||
assert switcheroo(False) is True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class {{ e.name() }}(enum.Enum): | ||
class {{ e.name()|class_name_py }}(enum.Enum): | ||
{% for variant in e.variants() -%} | ||
{{ variant|enum_name_py }} = {{ loop.index }} | ||
{% endfor -%} | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 3 additions & 40 deletions
43
uniffi_bindgen/src/bindings/python/templates/RecordTemplate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,15 @@ | ||
class {{ rec.name() }}(object): | ||
class {{ rec.name()|class_name_py }}(object): | ||
def __init__(self,{% for field in rec.fields() %}{{ field.name()|var_name_py }}{% if loop.last %}{% else %}, {% endif %}{% endfor %}): | ||
{%- for field in rec.fields() %} | ||
self.{{ field.name()|var_name_py }} = {{ field.name()|var_name_py }} | ||
{%- endfor %} | ||
|
||
def __str__(self): | ||
return "{{ rec.name() }}({% for field in rec.fields() %}{{ field.name() }}={}{% if loop.last %}{% else %}, {% endif %}{% endfor %})".format({% for field in rec.fields() %}self.{{ field.name() }}{% if loop.last %}{% else %}, {% endif %}{% endfor %}) | ||
return "{{ rec.name()|class_name_py }}({% for field in rec.fields() %}{{ field.name() }}={}{% if loop.last %}{% else %}, {% endif %}{% endfor %})".format({% for field in rec.fields() %}self.{{ field.name() }}{% if loop.last %}{% else %}, {% endif %}{% endfor %}) | ||
|
||
def __eq__(self, other): | ||
{%- for field in rec.fields() %} | ||
if self.{{ field.name()|var_name_py }} != other.{{ field.name()|var_name_py }}: | ||
return False | ||
return True | ||
{%- endfor %} | ||
|
||
@classmethod | ||
def _coerce(cls, v): | ||
# TODO: maybe we could do a bit of duck-typing here, details TBD | ||
assert isinstance(v, {{ rec.name() }}) | ||
return v | ||
|
||
@classmethod | ||
def _lift(cls, rbuf): | ||
return cls._liftFrom(RustBufferStream(rbuf)) | ||
|
||
@classmethod | ||
def _liftFrom(cls, buf): | ||
return cls( | ||
{%- for field in rec.fields() %} | ||
{{ "buf"|lift_from_py(field.type_()) }}{% if loop.last %}{% else %},{% endif %} | ||
{%- endfor %} | ||
) | ||
|
||
@classmethod | ||
def _lower(cls, v): | ||
rbuf = RustBuffer.alloc(cls._lowersIntoSize(v)) | ||
cls._lowerInto(v, RustBufferStream(rbuf)) | ||
return rbuf | ||
|
||
@classmethod | ||
def _lowersIntoSize(cls, v): | ||
return 0 + \ | ||
{%- for field in rec.fields() %} | ||
{{ "(v.{})"|format(field.name())|lowers_into_size_py(field.type_()) }}{% if loop.last %}{% else %} + \{% endif %} | ||
{%- endfor %} | ||
|
||
@classmethod | ||
def _lowerInto(cls, v, buf): | ||
{%- for field in rec.fields() %} | ||
{{ "(v.{})"|format(field.name())|lower_into_py("buf", field.type_()) }} | ||
{%- endfor %} | ||
{%- endfor %} |
Oops, something went wrong.