Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/python/templates/RustBufferHelper.py

raw
1 # Types conforming to `_UniffiConverterPrimitive` pass themselves directly over the FFI.
2 class _UniffiConverterPrimitive:
3 @classmethod
4 def lift(cls, value):
5 return value
6
7 @classmethod
8 def lower(cls, value):
9 return value
10
11 class _UniffiConverterPrimitiveInt(_UniffiConverterPrimitive):
12 @classmethod
13 def check_lower(cls, value):
14 try:
15 value = value.__index__()
16 except Exception:
17 raise TypeError("'{}' object cannot be interpreted as an integer".format(type(value).__name__))
18 if not isinstance(value, int):
19 raise TypeError("__index__ returned non-int (type {})".format(type(value).__name__))
20 if not cls.VALUE_MIN <= value < cls.VALUE_MAX:
21 raise ValueError("{} requires {} <= value < {}".format(cls.CLASS_NAME, cls.VALUE_MIN, cls.VALUE_MAX))
22
23 class _UniffiConverterPrimitiveFloat(_UniffiConverterPrimitive):
24 @classmethod
25 def check_lower(cls, value):
26 try:
27 value = value.__float__()
28 except Exception:
29 raise TypeError("must be real number, not {}".format(type(value).__name__))
30 if not isinstance(value, float):
31 raise TypeError("__float__ returned non-float (type {})".format(type(value).__name__))
32
33 # Helper class for wrapper types that will always go through a _UniffiRustBuffer.
34 # Classes should inherit from this and implement the `read` and `write` static methods.
35 class _UniffiConverterRustBuffer:
36 @classmethod
37 def lift(cls, rbuf):
38 with rbuf.consume_with_stream() as stream:
39 return cls.read(stream)
40
41 @classmethod
42 def lower(cls, value):
43 with _UniffiRustBuffer.alloc_with_builder() as builder:
44 cls.write(value, builder)
45 return builder.finalize()