跳轉至

API 參考

Python

ReadableBuffer 只供型別檢查器使用:它代表任何支援 buffer protocol 的物件(例如 bytes、bytearray 或 memoryview),執行期無法從 fastbase91 匯入。型別檢查器接受任何這類物件,但執行期其中一些會拋出 BufferError(見下表)。使用範例見使用方式。

from typing import Protocol

__version__: str
CORE_VERSION: str


class ReadableBuffer(Protocol):
    def __buffer__(self, flags: int, /) -> memoryview: ...


class DecodeError(ValueError):
    """A ``ValueError`` subclass raised in strict mode for a byte outside the basE91 alphabet.

    ``offset`` is the byte's 0-based position; for :class:`Decoder`, it is measured from the start of the whole stream. ``byte`` is the byte's value.
    """

    offset: int
    byte: int


def encode(data: ReadableBuffer, /) -> bytes:
    """Encode ``data`` as basE91 and return ``bytes``.

    The caller must not mutate a writable input buffer concurrently during this call.
    """


def decode(data: ReadableBuffer, /, *, strict: bool = False) -> bytes:
    """Decode basE91 ``data`` and return ``bytes``.

    Bytes outside the basE91 alphabet are ignored by default; in ``strict=True`` mode, the first such byte raises :class:`DecodeError`.

    The caller must not mutate a writable input buffer concurrently during this call.
    """


class Encoder:
    """Incrementally encode input as basE91.

    Call ``update()`` for each input chunk, then call ``finish()`` once at the end of the message.
    """

    def __init__(self) -> None: ...

    def update(self, data: ReadableBuffer, /) -> bytes:
        """Encode the next input chunk and return the available output.

        The caller must not mutate a writable input buffer concurrently during this call.
        """

    def finish(self) -> bytes:
        """Return the message's final output as ``bytes`` and close this encoder.

        Later calls to ``update()`` or ``finish()`` raise ``ValueError``.
        """


class Decoder:
    """Incrementally decode basE91 input.

    Bytes outside the basE91 alphabet are ignored by default; in ``strict=True`` mode, the first such byte raises :class:`DecodeError`.
    """

    def __init__(self, *, strict: bool = False) -> None: ...

    def update(self, data: ReadableBuffer, /) -> bytes:
        """Decode the next input chunk and return the available output.

        When strict mode rejects a byte, the call produces no output and leaves the decoder state unchanged.

        The caller must not mutate a writable input buffer concurrently during this call.
        """

    def finish(self) -> bytes:
        """Return the final decoded byte, if any, as ``bytes`` and close this decoder.

        Later calls to ``update()`` or ``finish()`` raise ``ValueError``.
        """

例外

例外 發生時機
DecodeError 只有在嚴格模式下,解碼遇到 basE91 字母表以外的位元組時發生。它是 ValueError 的子類別;offset 與 byte 屬性會指出該無效位元組。對一次性 decode 而言,offset 是輸入內的位置;對 Decoder(strict=True).update() 而言,offset 包含先前更新已消耗的位元組,是整個串流中的位置。
TypeError encode、decode 或 update() 收到不支援 buffer protocol 的物件,例如 str。
BufferError buffer 不是一維且連續,或格式不是恰好 B 或 b(無號或有號位元組)。例如 ctypes 陣列就會拋出它;請改傳 bytes(obj)。
ValueError finish() 關閉 Encoder 或 Decoder 後,再對該物件呼叫 update() 或 finish() 時發生 ValueError。
MemoryError 無法配置輸入或輸出 buffer,或 Rust core 回報 AllocationFailed。
RuntimeError Rust core 回報不應發生的內部錯誤;或者同一個 Encoder 或 Decoder 物件被兩條執行緒同時使用而發生借用衝突,後者可能拋出 RuntimeError。見自由執行緒。

Rust

完整的 Rust API 參考由原始碼產生,發佈於 docs.rs。

錯誤

錯誤型別或 variant 回傳它的 API 出錯時的狀態
OutputTooSmall encode_into、Encoder::update 所需大小可由 required() 取得。容量會先檢查,因此狀態與輸出都不變。
EncodeError::AllocationFailed 啟用 alloc 的 encode() 由一次性配置路徑回報。
DecodeError::OutputTooSmall decode_into、Decoder::update 容量會先檢查,因此狀態與輸出都不變。
DecodeError::InvalidByte { byte, offset } 嚴格模式下的 decode(需要 alloc)、decode_into、Decoder::update 解碼器狀態不變;失敗呼叫寫入的輸出必須丟棄。對 Decoder::update 而言,offset 是該區塊內的位置;對一次性 decode 與 decode_into 而言,是完整輸入內的位置。
DecodeError::AllocationFailed 啟用 alloc 的 decode() 由一次性配置路徑回報。

一次性的 encode()、decode() 函式與兩個 AllocationFailed variant 都只在啟用 alloc feature 時存在;預設的 std feature 會啟用它。encode() 回傳 EncodeError;由於它事先依 max_encoded_len 的上界預留足夠容量,實務上只會遇到 AllocationFailed。Encoder::finish 回傳 ([u8; 2], usize),不會失敗。Decoder::finish 回傳 Result<Option<u8>, DecodeError>。