| 1 |
|
| 2 |
class _UniffiRustBuffer(ctypes.Structure): |
| 3 |
_fields_ = [ |
| 4 |
("capacity", ctypes.c_uint64), |
| 5 |
("len", ctypes.c_uint64), |
| 6 |
("data", ctypes.POINTER(ctypes.c_char)), |
| 7 |
] |
| 8 |
|
| 9 |
@staticmethod |
| 10 |
def default(): |
| 11 |
return _UniffiRustBuffer(0, 0, None) |
| 12 |
|
| 13 |
@staticmethod |
| 14 |
def alloc(size): |
| 15 |
return _uniffi_rust_call(_UniffiLib.{{ ci.ffi_rustbuffer_alloc().name() }}, size) |
| 16 |
|
| 17 |
@staticmethod |
| 18 |
def reserve(rbuf, additional): |
| 19 |
return _uniffi_rust_call(_UniffiLib.{{ ci.ffi_rustbuffer_reserve().name() }}, rbuf, additional) |
| 20 |
|
| 21 |
def free(self): |
| 22 |
return _uniffi_rust_call(_UniffiLib.{{ ci.ffi_rustbuffer_free().name() }}, self) |
| 23 |
|
| 24 |
def __str__(self): |
| 25 |
return "_UniffiRustBuffer(capacity={}, len={}, data={})".format( |
| 26 |
self.capacity, |
| 27 |
self.len, |
| 28 |
self.data[0:self.len] |
| 29 |
) |
| 30 |
|
| 31 |
@contextlib.contextmanager |
| 32 |
def alloc_with_builder(*args): |
| 33 |
"""Context-manger to allocate a buffer using a _UniffiRustBufferBuilder. |
| 34 |
|
| 35 |
The allocated buffer will be automatically freed if an error occurs, ensuring that |
| 36 |
we don't accidentally leak it. |
| 37 |
""" |
| 38 |
builder = _UniffiRustBufferBuilder() |
| 39 |
try: |
| 40 |
yield builder |
| 41 |
except: |
| 42 |
builder.discard() |
| 43 |
raise |
| 44 |
|
| 45 |
@contextlib.contextmanager |
| 46 |
def consume_with_stream(self): |
| 47 |
"""Context-manager to consume a buffer using a _UniffiRustBufferStream. |
| 48 |
|
| 49 |
The _UniffiRustBuffer will be freed once the context-manager exits, ensuring that we don't |
| 50 |
leak it even if an error occurs. |
| 51 |
""" |
| 52 |
try: |
| 53 |
s = _UniffiRustBufferStream.from_rust_buffer(self) |
| 54 |
yield s |
| 55 |
if s.remaining() != 0: |
| 56 |
raise RuntimeError("junk data left in buffer at end of consume_with_stream") |
| 57 |
finally: |
| 58 |
self.free() |
| 59 |
|
| 60 |
@contextlib.contextmanager |
| 61 |
def read_with_stream(self): |
| 62 |
"""Context-manager to read a buffer using a _UniffiRustBufferStream. |
| 63 |
|
| 64 |
This is like consume_with_stream, but doesn't free the buffer afterwards. |
| 65 |
It should only be used with borrowed `_UniffiRustBuffer` data. |
| 66 |
""" |
| 67 |
s = _UniffiRustBufferStream.from_rust_buffer(self) |
| 68 |
yield s |
| 69 |
if s.remaining() != 0: |
| 70 |
raise RuntimeError("junk data left in buffer at end of read_with_stream") |
| 71 |
|
| 72 |
class _UniffiForeignBytes(ctypes.Structure): |
| 73 |
_fields_ = [ |
| 74 |
("len", ctypes.c_int32), |
| 75 |
("data", ctypes.POINTER(ctypes.c_char)), |
| 76 |
] |
| 77 |
|
| 78 |
def __str__(self): |
| 79 |
return "_UniffiForeignBytes(len={}, data={})".format(self.len, self.data[0:self.len]) |
| 80 |
|
| 81 |
|
| 82 |
class _UniffiRustBufferStream: |
| 83 |
""" |
| 84 |
Helper for structured reading of bytes from a _UniffiRustBuffer |
| 85 |
""" |
| 86 |
|
| 87 |
def __init__(self, data, len): |
| 88 |
self.data = data |
| 89 |
self.len = len |
| 90 |
self.offset = 0 |
| 91 |
|
| 92 |
@classmethod |
| 93 |
def from_rust_buffer(cls, buf): |
| 94 |
return cls(buf.data, buf.len) |
| 95 |
|
| 96 |
def remaining(self): |
| 97 |
return self.len - self.offset |
| 98 |
|
| 99 |
def _unpack_from(self, size, format): |
| 100 |
if self.offset + size > self.len: |
| 101 |
raise InternalError("read past end of rust buffer") |
| 102 |
value = struct.unpack(format, self.data[self.offset:self.offset+size])[0] |
| 103 |
self.offset += size |
| 104 |
return value |
| 105 |
|
| 106 |
def read(self, size): |
| 107 |
if self.offset + size > self.len: |
| 108 |
raise InternalError("read past end of rust buffer") |
| 109 |
data = self.data[self.offset:self.offset+size] |
| 110 |
self.offset += size |
| 111 |
return data |
| 112 |
|
| 113 |
def read_i8(self): |
| 114 |
return self._unpack_from(1, ">b") |
| 115 |
|
| 116 |
def read_u8(self): |
| 117 |
return self._unpack_from(1, ">B") |
| 118 |
|
| 119 |
def read_i16(self): |
| 120 |
return self._unpack_from(2, ">h") |
| 121 |
|
| 122 |
def read_u16(self): |
| 123 |
return self._unpack_from(2, ">H") |
| 124 |
|
| 125 |
def read_i32(self): |
| 126 |
return self._unpack_from(4, ">i") |
| 127 |
|
| 128 |
def read_u32(self): |
| 129 |
return self._unpack_from(4, ">I") |
| 130 |
|
| 131 |
def read_i64(self): |
| 132 |
return self._unpack_from(8, ">q") |
| 133 |
|
| 134 |
def read_u64(self): |
| 135 |
return self._unpack_from(8, ">Q") |
| 136 |
|
| 137 |
def read_float(self): |
| 138 |
v = self._unpack_from(4, ">f") |
| 139 |
return v |
| 140 |
|
| 141 |
def read_double(self): |
| 142 |
return self._unpack_from(8, ">d") |
| 143 |
|
| 144 |
class _UniffiRustBufferBuilder: |
| 145 |
""" |
| 146 |
Helper for structured writing of bytes into a _UniffiRustBuffer. |
| 147 |
""" |
| 148 |
|
| 149 |
def __init__(self): |
| 150 |
self.rbuf = _UniffiRustBuffer.alloc(16) |
| 151 |
self.rbuf.len = 0 |
| 152 |
|
| 153 |
def finalize(self): |
| 154 |
rbuf = self.rbuf |
| 155 |
self.rbuf = None |
| 156 |
return rbuf |
| 157 |
|
| 158 |
def discard(self): |
| 159 |
if self.rbuf is not None: |
| 160 |
rbuf = self.finalize() |
| 161 |
rbuf.free() |
| 162 |
|
| 163 |
@contextlib.contextmanager |
| 164 |
def _reserve(self, num_bytes): |
| 165 |
if self.rbuf.len + num_bytes > self.rbuf.capacity: |
| 166 |
self.rbuf = _UniffiRustBuffer.reserve(self.rbuf, num_bytes) |
| 167 |
yield None |
| 168 |
self.rbuf.len += num_bytes |
| 169 |
|
| 170 |
def _pack_into(self, size, format, value): |
| 171 |
with self._reserve(size): |
| 172 |
# XXX TODO: I feel like I should be able to use `struct.pack_into` here but can't figure it out. |
| 173 |
for i, byte in enumerate(struct.pack(format, value)): |
| 174 |
self.rbuf.data[self.rbuf.len + i] = byte |
| 175 |
|
| 176 |
def write(self, value): |
| 177 |
with self._reserve(len(value)): |
| 178 |
for i, byte in enumerate(value): |
| 179 |
self.rbuf.data[self.rbuf.len + i] = byte |
| 180 |
|
| 181 |
def write_i8(self, v): |
| 182 |
self._pack_into(1, ">b", v) |
| 183 |
|
| 184 |
def write_u8(self, v): |
| 185 |
self._pack_into(1, ">B", v) |
| 186 |
|
| 187 |
def write_i16(self, v): |
| 188 |
self._pack_into(2, ">h", v) |
| 189 |
|
| 190 |
def write_u16(self, v): |
| 191 |
self._pack_into(2, ">H", v) |
| 192 |
|
| 193 |
def write_i32(self, v): |
| 194 |
self._pack_into(4, ">i", v) |
| 195 |
|
| 196 |
def write_u32(self, v): |
| 197 |
self._pack_into(4, ">I", v) |
| 198 |
|
| 199 |
def write_i64(self, v): |
| 200 |
self._pack_into(8, ">q", v) |
| 201 |
|
| 202 |
def write_u64(self, v): |
| 203 |
self._pack_into(8, ">Q", v) |
| 204 |
|
| 205 |
def write_float(self, v): |
| 206 |
self._pack_into(4, ">f", v) |
| 207 |
|
| 208 |
def write_double(self, v): |
| 209 |
self._pack_into(8, ">d", v) |
| 210 |
|
| 211 |
def write_c_size_t(self, v): |
| 212 |
self._pack_into(ctypes.sizeof(ctypes.c_size_t) , "@N", v) |