1 # Types conforming to `_UniffiConverterPrimitive` pass themselves directly over the FFI.
2 class _UniffiConverterPrimitive
:
12 def lower(cls
, value
):
13 return cls
.lowerUnchecked(cls
.check(value
))
16 def lowerUnchecked(cls
, value
):
20 def write(cls
, value
, buf
):
21 cls
.write_unchecked(cls
.check(value
), buf
)
23 class _UniffiConverterPrimitiveInt(_UniffiConverterPrimitive
):
25 def check(cls
, value
):
27 value
= value
.__index
__()
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
):
38 def check(cls
, value
):
40 value
= value
.__float
__()
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
:
52 with rbuf
.consume_with_stream() as stream
:
53 return cls
.read(stream
)
56 def lower(cls
, value
):
57 with _UniffiRustBuffer
.alloc_with_builder() as builder
:
58 cls
.write(value
, builder
)
59 return builder
.finalize()