rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
Documentation
//! Handle-based FFI API for relativity functions.

use crate::symbolic::core::Expr;
use crate::symbolic::relativity;

/// Structure to hold two expressions (e.g., transformed x and t).
#[repr(C)]
pub struct ExprPair {
    /// The first expression in the pair.
    pub first: *mut Expr,
    /// The second expression in the pair.
    pub second: *mut Expr,
}

/// Calculates the Lorentz factor.
///
/// # 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_lorentz_factor(velocity: *const Expr) -> *mut Expr {
    unsafe {
        if velocity.is_null() {
            return std::ptr::null_mut();
        }

        Box::into_raw(Box::new(relativity::lorentz_factor(&*velocity)))
    }
}

/// Performs a Lorentz transformation in the x-direction.
///
/// # 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_lorentz_transformation_x(
    x: *const Expr,
    t: *const Expr,
    v: *const Expr,
) -> ExprPair {
    unsafe {
        if x.is_null() || t.is_null() || v.is_null() {
            return ExprPair {
                first: std::ptr::null_mut(),
                second: std::ptr::null_mut(),
            };
        }

        let (xp, tp) = relativity::lorentz_transformation_x(&*x, &*t, &*v);

        ExprPair {
            first: Box::into_raw(Box::new(xp)),
            second: Box::into_raw(Box::new(tp)),
        }
    }
}

/// Calculates mass-energy equivalence.
///
/// # 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_mass_energy_equivalence(mass: *const Expr) -> *mut Expr {
    unsafe {
        if mass.is_null() {
            return std::ptr::null_mut();
        }

        Box::into_raw(Box::new(relativity::mass_energy_equivalence(&*mass)))
    }
}

/// Calculates Schwarzschild radius.
///
/// # 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_schwarzschild_radius(mass: *const Expr) -> *mut Expr {
    unsafe {
        if mass.is_null() {
            return std::ptr::null_mut();
        }

        Box::into_raw(Box::new(relativity::schwarzschild_radius(&*mass)))
    }
}