tflite-c-rs 0.0.1

Safe Rust wrapper for the TensorFlow Lite C API via runtime dynamic loading (libloading) — no build-time TFLite link required.
Documentation
//! Raw FFI types and function-signature aliases for the TensorFlow Lite C API.
//!
//! The opaque types here are zero-sized stand-ins for handles whose layouts
//! live entirely on the C side of the boundary. They are exposed publicly so
//! that the safe wrapper types can name them in their signatures, but users
//! of this crate should never need to construct or dereference them directly.

#![allow(non_camel_case_types)]

use std::os::raw::{c_char, c_int, c_void};

/// Opaque handle to a `TfLiteModel`. See `TfLiteModelCreateFromFile` upstream.
#[repr(C)]
pub struct TfLiteModel {
    _private: [u8; 0],
}

/// Opaque handle to a `TfLiteInterpreterOptions`.
#[repr(C)]
pub struct TfLiteInterpreterOptions {
    _private: [u8; 0],
}

/// Opaque handle to a `TfLiteInterpreter`.
#[repr(C)]
pub struct TfLiteInterpreter {
    _private: [u8; 0],
}

/// Opaque handle to a `TfLiteTensor`.
#[repr(C)]
pub struct TfLiteTensor {
    _private: [u8; 0],
}

/// Opaque handle to a `TfLiteDelegate` (any delegate kind).
#[repr(C)]
pub struct TfLiteDelegate {
    _private: [u8; 0],
}

/// Opaque handle to a `TfLiteExternalDelegateOptions`.
#[repr(C)]
pub struct TfLiteExternalDelegateOptions {
    _private: [u8; 0],
}

/// Return status of fallible TFLite C entry points.
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum TfLiteStatus {
    /// Operation succeeded.
    Ok = 0,
    /// A generic error occurred.
    Error = 1,
    /// A delegate-related error occurred.
    DelegateError = 2,
    /// Application-side error (e.g. invalid arguments).
    ApplicationError = 3,
    /// A delegate's `Prepare` step failed.
    DelegateDataNotFound = 4,
    /// A delegate's write of data failed.
    DelegateDataWriteError = 5,
    /// A delegate's read of data failed.
    DelegateDataReadError = 6,
    /// An operator was not resolved.
    UnresolvedOps = 7,
    /// The operation was cancelled.
    Cancelled = 8,
    /// An output tensor's shape was resized.
    OutputShapeNotKnown = 9,
}

/// Tensor element type, mirroring the upstream `TfLiteType` enum.
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum TfLiteType {
    /// No type / unset.
    NoType = 0,
    /// 32-bit IEEE float.
    Float32 = 1,
    /// 32-bit signed integer.
    Int32 = 2,
    /// 8-bit unsigned integer.
    UInt8 = 3,
    /// 64-bit signed integer.
    Int64 = 4,
    /// UTF-8 string.
    String = 5,
    /// Boolean.
    Bool = 6,
    /// 16-bit signed integer.
    Int16 = 7,
    /// 64-bit complex (two f32s).
    Complex64 = 8,
    /// 8-bit signed integer.
    Int8 = 9,
    /// 16-bit IEEE float.
    Float16 = 10,
    /// 64-bit IEEE float.
    Float64 = 11,
    /// 128-bit complex (two f64s).
    Complex128 = 12,
    /// 64-bit unsigned integer.
    UInt64 = 13,
    /// Variable-length resource handle.
    Resource = 14,
    /// Opaque variant.
    Variant = 15,
    /// 32-bit unsigned integer.
    UInt32 = 16,
    /// 16-bit unsigned integer.
    UInt16 = 17,
    /// 4-bit signed integer.
    Int4 = 18,
    /// `bfloat16` (truncated 32-bit float).
    BFloat16 = 19,
}

/// Affine-quantization parameters: `real = (q - zero_point) * scale`.
#[repr(C)]
#[derive(Debug, Clone, Copy, Default)]
pub struct TfLiteQuantizationParams {
    /// Scale factor applied to the quantized value.
    pub scale: f32,
    /// Integer offset subtracted before scaling.
    pub zero_point: i32,
}

// --- Function-signature type aliases ------------------------------------------------
//
// These are kept as `type` aliases (rather than inlined into the symbol map) so that
// the `library` module can name them concisely when caching `Symbol` handles.

/// `TfLiteModel* TfLiteModelCreateFromFile(const char*)`
pub type ModelCreateFromFileFn = unsafe extern "C" fn(*const c_char) -> *mut TfLiteModel;
/// `void TfLiteModelDelete(TfLiteModel*)`
pub type ModelDeleteFn = unsafe extern "C" fn(*mut TfLiteModel);

/// `TfLiteInterpreterOptions* TfLiteInterpreterOptionsCreate()`
pub type OptionsCreateFn = unsafe extern "C" fn() -> *mut TfLiteInterpreterOptions;
/// `void TfLiteInterpreterOptionsDelete(TfLiteInterpreterOptions*)`
pub type OptionsDeleteFn = unsafe extern "C" fn(*mut TfLiteInterpreterOptions);
/// `void TfLiteInterpreterOptionsSetNumThreads(TfLiteInterpreterOptions*, int32_t)`
pub type OptionsSetNumThreadsFn =
    unsafe extern "C" fn(*mut TfLiteInterpreterOptions, c_int);
/// `void TfLiteInterpreterOptionsAddDelegate(TfLiteInterpreterOptions*, TfLiteDelegate*)`
pub type OptionsAddDelegateFn =
    unsafe extern "C" fn(*mut TfLiteInterpreterOptions, *mut TfLiteDelegate);

/// `TfLiteInterpreter* TfLiteInterpreterCreate(const TfLiteModel*, const TfLiteInterpreterOptions*)`
pub type InterpreterCreateFn = unsafe extern "C" fn(
    *const TfLiteModel,
    *const TfLiteInterpreterOptions,
) -> *mut TfLiteInterpreter;
/// `void TfLiteInterpreterDelete(TfLiteInterpreter*)`
pub type InterpreterDeleteFn = unsafe extern "C" fn(*mut TfLiteInterpreter);
/// `TfLiteStatus TfLiteInterpreterAllocateTensors(TfLiteInterpreter*)`
pub type InterpreterAllocateTensorsFn =
    unsafe extern "C" fn(*mut TfLiteInterpreter) -> TfLiteStatus;
/// `TfLiteStatus TfLiteInterpreterInvoke(TfLiteInterpreter*)`
pub type InterpreterInvokeFn = unsafe extern "C" fn(*mut TfLiteInterpreter) -> TfLiteStatus;

/// `int32_t TfLiteInterpreterGetInputTensorCount(const TfLiteInterpreter*)`
pub type InterpreterGetTensorCountFn =
    unsafe extern "C" fn(*const TfLiteInterpreter) -> c_int;
/// `TfLiteTensor* TfLiteInterpreterGetInputTensor(const TfLiteInterpreter*, int32_t)`
pub type InterpreterGetInputTensorFn =
    unsafe extern "C" fn(*const TfLiteInterpreter, c_int) -> *mut TfLiteTensor;
/// `const TfLiteTensor* TfLiteInterpreterGetOutputTensor(const TfLiteInterpreter*, int32_t)`
pub type InterpreterGetOutputTensorFn =
    unsafe extern "C" fn(*const TfLiteInterpreter, c_int) -> *const TfLiteTensor;

/// `TfLiteType TfLiteTensorType(const TfLiteTensor*)`
pub type TensorTypeFn = unsafe extern "C" fn(*const TfLiteTensor) -> TfLiteType;
/// `int32_t TfLiteTensorNumDims(const TfLiteTensor*)`
pub type TensorNumDimsFn = unsafe extern "C" fn(*const TfLiteTensor) -> c_int;
/// `int32_t TfLiteTensorDim(const TfLiteTensor*, int32_t)`
pub type TensorDimFn = unsafe extern "C" fn(*const TfLiteTensor, c_int) -> c_int;
/// `size_t TfLiteTensorByteSize(const TfLiteTensor*)`
pub type TensorByteSizeFn = unsafe extern "C" fn(*const TfLiteTensor) -> usize;
/// `void* TfLiteTensorData(const TfLiteTensor*)`
pub type TensorDataFn = unsafe extern "C" fn(*const TfLiteTensor) -> *mut c_void;
/// `const char* TfLiteTensorName(const TfLiteTensor*)`
pub type TensorNameFn = unsafe extern "C" fn(*const TfLiteTensor) -> *const c_char;
/// `TfLiteQuantizationParams TfLiteTensorQuantizationParams(const TfLiteTensor*)`
pub type TensorQuantizationParamsFn =
    unsafe extern "C" fn(*const TfLiteTensor) -> TfLiteQuantizationParams;

/// `TfLiteExternalDelegateOptions* TfLiteExternalDelegateOptionsCreate()`
pub type ExternalDelegateOptionsCreateFn =
    unsafe extern "C" fn() -> *mut TfLiteExternalDelegateOptions;
/// `void TfLiteExternalDelegateOptionsDelete(TfLiteExternalDelegateOptions*)`
pub type ExternalDelegateOptionsDeleteFn =
    unsafe extern "C" fn(*mut TfLiteExternalDelegateOptions);
/// `TfLiteStatus TfLiteExternalDelegateOptionsSetLibraryPath(TfLiteExternalDelegateOptions*, const char*)`
pub type ExternalDelegateOptionsSetLibraryPathFn =
    unsafe extern "C" fn(*mut TfLiteExternalDelegateOptions, *const c_char) -> TfLiteStatus;
/// `TfLiteStatus TfLiteExternalDelegateOptionsInsert(TfLiteExternalDelegateOptions*, const char*, const char*)`
pub type ExternalDelegateOptionsInsertFn = unsafe extern "C" fn(
    *mut TfLiteExternalDelegateOptions,
    *const c_char,
    *const c_char,
) -> TfLiteStatus;
/// `TfLiteDelegate* TfLiteExternalDelegateCreate(const TfLiteExternalDelegateOptions*)`
pub type ExternalDelegateCreateFn =
    unsafe extern "C" fn(*const TfLiteExternalDelegateOptions) -> *mut TfLiteDelegate;
/// `void TfLiteExternalDelegateDelete(TfLiteDelegate*)`
pub type ExternalDelegateDeleteFn = unsafe extern "C" fn(*mut TfLiteDelegate);