A data structure to collect stream contents efficiently in O(n).
| 37 | |
| 38 | |
| 39 | class ReceiveBuffer: |
| 40 | """ |
| 41 | A data structure to collect stream contents efficiently in O(n). |
| 42 | """ |
| 43 | |
| 44 | _chunks: list[bytes] |
| 45 | _len: int |
| 46 | |
| 47 | def __init__(self): |
| 48 | self._chunks = [] |
| 49 | self._len = 0 |
| 50 | |
| 51 | def __iadd__(self, other: bytes): |
| 52 | assert isinstance(other, bytes) |
| 53 | self._chunks.append(other) |
| 54 | self._len += len(other) |
| 55 | return self |
| 56 | |
| 57 | def __len__(self): |
| 58 | return self._len |
| 59 | |
| 60 | def __bytes__(self): |
| 61 | return b"".join(self._chunks) |
| 62 | |
| 63 | def __bool__(self): |
| 64 | return self._len > 0 |
| 65 | |
| 66 | def clear(self): |
| 67 | self._chunks.clear() |
| 68 | self._len = 0 |
no outgoing calls
searching dependent graphs…