Backed out 5 changesets (bug 1890092, bug 1888683) for causing build bustages & crash...
[gecko.git] / third_party / rust / uniffi_bindgen / src / bindings / python / templates / RustBufferHelper.py
blobdaabd5b4b9b3b7cffc1d213045a5cfef2c413fa7
1 # Types conforming to `_UniffiConverterPrimitive` pass themselves directly over the FFI.
2 class _UniffiConverterPrimitive:
3 @classmethod
4 def check(cls, value):
5 return value
7 @classmethod
8 def lift(cls, value):
9 return value
11 @classmethod
12 def lower(cls, value):
13 return cls.lowerUnchecked(cls.check(value))
15 @classmethod
16 def lowerUnchecked(cls, value):
17 return value
19 @classmethod
20 def write(cls, value, buf):
21 cls.write_unchecked(cls.check(value), buf)
23 class _UniffiConverterPrimitiveInt(_UniffiConverterPrimitive):
24 @classmethod
25 def check(cls, value):
26 try:
27 value = value.__index__()
28 except Exception:
29 raise TypeError("'{}' object cannot be interpreted as an integer".format(type(value).__name__))
30 if not isinstance(value, int):
31 raise TypeError("__index__ returned non-int (type {})".format(type(value).__name__))
32 if not cls.VALUE_MIN <= value < cls.VALUE_MAX:
33 raise ValueError("{} requires {} <= value < {}".format(cls.CLASS_NAME, cls.VALUE_MIN, cls.VALUE_MAX))
34 return super().check(value)
36 class _UniffiConverterPrimitiveFloat(_UniffiConverterPrimitive):
37 @classmethod
38 def check(cls, value):
39 try:
40 value = value.__float__()
41 except Exception:
42 raise TypeError("must be real number, not {}".format(type(value).__name__))
43 if not isinstance(value, float):
44 raise TypeError("__float__ returned non-float (type {})".format(type(value).__name__))
45 return super().check(value)
47 # Helper class for wrapper types that will always go through a _UniffiRustBuffer.
48 # Classes should inherit from this and implement the `read` and `write` static methods.
49 class _UniffiConverterRustBuffer:
50 @classmethod
51 def lift(cls, rbuf):
52 with rbuf.consume_with_stream() as stream:
53 return cls.read(stream)
55 @classmethod
56 def lower(cls, value):
57 with _UniffiRustBuffer.alloc_with_builder() as builder:
58 cls.write(value, builder)
59 return builder.finalize()