This directory contains a Rust library with various helper functions used throughout TIM. Currently, only basic and frequently used functions are defined, but over time the goal is to convert the entire document processing pipeline to use Rust.
The library includes Python bindings, which are used by TIM backend.
To build the library, use ./tim rust
command.
The command automatically builds and installs the library
in the tim_rust/python
path.
./tim update all
and other commands will automatically
invoke the Rust build if necessary.
tim_rust uses PyO3 to handle interop between Python and Rust.
-
Add the function signature to
python/tim_rust.pyi
. Define any documentation and type annotations for Python.Example:
def sum(a: int, b: int) -> int: """Returns the sum of two integers.""" ...
Instead of the method body, use
...
to indicate that the function is defined in Rust. -
Add the function to
python/src/lib.rs
. Use#[pyfunction]
attribute to mark the function as a Python function.Example:
#[pyfunction] fn sum(a: i64, b: i64) -> PyResult<i64> { Ok(a + b) }
Note that the return type is always PyResult, where T is the actual return type of the function. Refer to PyO3 type conversion chart to see how to convert between Python and Rust types.
-
Add the function to
tim_rust
module withadd_function
function inpython/src/lib.rs
:#[pymodule] fn tim_rust(_py: Python, m: &PyModule) -> PyResult<()> { // Other functions... m.add_function(wrap_pyfunction!(sum, m)?)?; // Other functions... Ok(()) }