Skip to main content

edgefirst_tflite/
library.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 Au-Zone Technologies. All Rights Reserved.
3
4//! Safe wrapper around the `TFLite` shared-library handle.
5
6use std::fmt;
7use std::path::Path;
8
9use crate::error::{Error, Result};
10
11/// Handle to a loaded `TFLite` shared library.
12///
13/// `Library` wraps the FFI function table produced by `libloading` and
14/// `bindgen`, providing safe construction via auto-discovery or an explicit
15/// filesystem path.
16///
17/// # Examples
18///
19/// ```no_run
20/// use edgefirst_tflite::Library;
21///
22/// // Auto-discover TFLite library
23/// let lib = Library::new()?;
24///
25/// // Or load from a specific path
26/// let lib = Library::from_path("/usr/lib/libtensorflowlite_c.so")?;
27/// # Ok::<(), edgefirst_tflite::Error>(())
28/// ```
29pub struct Library {
30    inner: edgefirst_tflite_sys::tensorflowlite_c,
31}
32
33impl Library {
34    /// Discover and load the `TFLite` shared library automatically.
35    ///
36    /// This probes well-known versioned and unversioned library paths using
37    /// [`edgefirst_tflite_sys::discovery::discover`].
38    ///
39    /// # Errors
40    ///
41    /// Returns an [`Error`] if no compatible `TFLite` library can be found.
42    pub fn new() -> Result<Self> {
43        let inner = edgefirst_tflite_sys::discovery::discover().map_err(Error::from)?;
44        Ok(Self { inner })
45    }
46
47    /// Load the `TFLite` shared library from a specific `path`.
48    ///
49    /// # Errors
50    ///
51    /// Returns an [`Error`] if the library cannot be loaded from `path` or
52    /// required symbols are missing.
53    pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {
54        let inner = edgefirst_tflite_sys::discovery::load(path).map_err(Error::from)?;
55        Ok(Self { inner })
56    }
57
58    /// Returns a reference to the underlying FFI function table.
59    ///
60    /// This is an escape hatch for advanced users who need direct access to
61    /// the raw `tensorflowlite_c` bindings.
62    #[must_use]
63    pub fn as_sys(&self) -> &edgefirst_tflite_sys::tensorflowlite_c {
64        &self.inner
65    }
66}
67
68impl fmt::Debug for Library {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.debug_struct("Library")
71            .field("inner", &"tensorflowlite_c { .. }")
72            .finish()
73    }
74}