Skip to main content

Library

Struct Library 

Source
pub struct Library { /* private fields */ }
Expand description

A loaded KunQuant library containing compiled factor modules.

A Library represents a dynamically loaded shared library (.so/.dll/.dylib) that contains one or more compiled factor computation modules. These libraries are typically generated using KunQuant’s Python compilation interface.

§Library Structure

Each library can contain multiple named modules, where each module represents a complete factor computation graph with defined inputs and outputs.

§Memory Management

The library automatically manages its resources using RAII. The underlying C handle and loaded library are properly cleaned up when dropped.

§Thread Safety

Libraries are thread-safe and can be shared across multiple threads. Multiple modules can be retrieved and used concurrently from the same library.

Implementations§

Source§

impl Library

Source

pub fn load<P: AsRef<str>>(path: P) -> Result<Self>

Loads a KunQuant factor library from the specified file path.

This method dynamically loads a compiled factor library and makes its modules available for computation. The library must be compiled with compatible settings for the intended computation mode (batch vs streaming).

§Arguments
  • path - Path to the compiled library file. Can be any type that implements AsRef<str> (e.g., &str, String, PathBuf, etc.)
§Returns

Returns Ok(Library) on successful loading, or an error if:

  • The file doesn’t exist or isn’t accessible
  • The file isn’t a valid KunQuant library
  • The library has incompatible version or architecture
  • System resources are insufficient for loading
§Examples
use kunquant_rs::Library;

// Load library from relative path
let library = Library::load("factors/my_factors.so")?;

// Load library from absolute path
let library = Library::load("/opt/factors/alpha_factors.so")?;

// Works with String as well
let path = String::from("./test_factors.so");
let library = Library::load(path)?;
§Library Requirements
  • Must be compiled with compatible KunQuant version
  • Architecture must match the target platform
  • All dependencies must be available in the system
  • File must have appropriate read permissions
§Performance Notes
  • Library loading is a one-time cost during initialization
  • Loaded libraries are cached by the system loader
  • Multiple Library instances of the same file share underlying resources
Source

pub fn get_module<N: AsRef<str>>(&self, name: N) -> Result<Module<'_>>

Retrieves a named factor module from the loaded library.

Each library can contain multiple factor modules, each representing a complete computation graph with defined inputs and outputs. This method provides access to a specific module by name.

§Arguments
  • name - The name of the module as defined during compilation. Can be any type that implements AsRef<str> (e.g., &str, String, etc.)
§Returns

Returns Ok(Module) on success, or an error if:

  • No module with the specified name exists in the library
  • The library handle is invalid
  • The C library call fails
§Examples
use kunquant_rs::Library;

let library = Library::load("factors.so")?;

// Get a specific factor module
let alpha001 = library.get_module("alpha001")?;
let momentum = library.get_module("momentum_factor")?;

// Works with String as well
let module_name = String::from("mean_reversion");
let mean_rev = library.get_module(module_name)?;
§Module Naming
  • Module names are defined during factor compilation
  • Names are case-sensitive and must match exactly
  • Common naming conventions include factor names (e.g., “alpha001”) or descriptive names (e.g., “price_momentum”, “volume_weighted_return”)
§Lifetime Management

The returned Module maintains a reference to the parent Library, ensuring the library remains loaded for the module’s lifetime.

Trait Implementations§

Source§

impl Drop for Library

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.