rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
Documentation
//! Bincode-based FFI API for numerical combinatorics.

use serde::Deserialize;

use crate::ffi_apis::common::BincodeBuffer;
use crate::ffi_apis::common::from_bincode_buffer;
use crate::ffi_apis::common::to_bincode_buffer;
use crate::ffi_apis::ffi_api::FfiResult;
use crate::numerical::combinatorics;

#[derive(Deserialize)]
struct NInput {
    n: u64,
}

#[derive(Deserialize)]
struct NKInput {
    n: u64,
    k: u64,
}

#[derive(Deserialize)]
struct RecurrenceInput {
    coeffs: Vec<f64>,
    initial_conditions: Vec<f64>,
    target_n: usize,
}

/// Computes the factorial of a number using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_factorial_bincode(buffer: BincodeBuffer) -> BincodeBuffer {
    let input: NInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::factorial(input.n);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}

/// Computes the number of permutations using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_permutations_bincode(
    buffer: BincodeBuffer
) -> BincodeBuffer {
    let input: NKInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::permutations(input.n, input.k);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}

/// Computes the number of combinations using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_combinations_bincode(
    buffer: BincodeBuffer
) -> BincodeBuffer {
    let input: NKInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::combinations(input.n, input.k);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}

/// Solves a linear recurrence relation using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_solve_recurrence_bincode(
    buffer: BincodeBuffer
) -> BincodeBuffer {
    let input: RecurrenceInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    match combinatorics::solve_recurrence_numerical(
        &input.coeffs,
        &input.initial_conditions,
        input.target_n,
    ) {
        | Ok(res) => {
            to_bincode_buffer(&FfiResult {
                ok: Some(res),
                err: None::<String>,
            })
        },
        | Err(e) => {
            to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some(e),
            })
        },
    }
}

#[derive(Deserialize)]
struct XNInput {
    x: f64,
    n: u64,
}

/// Computes the Stirling number of the second kind using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_stirling_second_bincode(
    buffer: BincodeBuffer
) -> BincodeBuffer {
    let input: NKInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::stirling_second(input.n, input.k);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}

/// Computes the Bell number using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_bell_bincode(buffer: BincodeBuffer) -> BincodeBuffer {
    let input: NInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::bell(input.n);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}

/// Computes the Catalan number using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_catalan_bincode(buffer: BincodeBuffer) -> BincodeBuffer {
    let input: NInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::catalan(input.n);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}

/// Computes the rising factorial using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_rising_factorial_bincode(
    buffer: BincodeBuffer
) -> BincodeBuffer {
    let input: XNInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::rising_factorial(input.x, input.n);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}

/// Computes the falling factorial using bincode for serialization.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_num_comb_falling_factorial_bincode(
    buffer: BincodeBuffer
) -> BincodeBuffer {
    let input: XNInput = match from_bincode_buffer(&buffer) {
        | Some(i) => i,
        | None => {
            return to_bincode_buffer(&FfiResult::<f64, String> {
                ok: None,
                err: Some("Invalid Bincode input".to_string()),
            });
        },
    };

    let res = combinatorics::falling_factorial(input.x, input.n);

    to_bincode_buffer(&FfiResult {
        ok: Some(res),
        err: None::<String>,
    })
}