Rust binding ============ ``rust/gpridge-sys`` is a -sys crate: raw declarations of the :doc:`C API ` that bindgen generates from ``gpr/capi/gpridge_c.h`` when the crate is built, linked against an installed ``libgpridge``. It does not compile the C++ core and has no runtime dependencies. It is not on crates.io, and no CI job publishes it; use it as a path or git dependency. Build and install the library first and select its installation as described in :doc:`Native packages and downstream bindings <../../howto/native_packages>`. How the crate finds the library ------------------------------- ``build.rs`` tries these in order: 1. pkg-config for ``gpridge`` version 0.6.0 or later. The header is ``gpr/capi/gpridge_c.h`` under one of the reported include paths, or the copy in this checkout when none holds it. 2. The environment variables ``GPRIDGE_LIBDIR`` (the library directory) and ``GPRIDGE_INCLUDE`` (an include directory or the path of the header). The header defaults to the copy in this checkout. 3. Otherwise the build stops with a message that names both routes. ``build.rs`` adds the library directory to the link search path. It writes the directory into the rpath only for the crate's own test binaries. A program that depends on the crate has no such rpath: ``cargo run`` and ``cargo test`` put the directory on the loader path while they run, and the built program run outside Cargo needs the library directory on ``LD_LIBRARY_PATH``. The example program below, run with an empty environment (``env -i``), stopped with ``error while loading shared libraries: libgpridge.so: cannot open shared object file``. bindgen needs libclang. When ``LIBCLANG_PATH`` is unset, ``build.rs`` sets it to the last ``/usr/lib/llvm-*/lib`` directory, in name order, that holds ``libclang.so``. Set the variable yourself when libclang is elsewhere. The crate exposes the functions, types and constants whose names start with ``gpr_`` or ``GPR_``. bindgen does not translate function-like macros, so ``GPR_ABI_CHECK()`` is not available. Call ``gpr_abi_check`` with ``GPR_C_ABI_MAJOR`` and ``GPR_C_ABI_MINOR`` instead, once at start, as the crate's smoke test does. The crate does not call it for you. The ``gpridge_sys::safe`` module provides owned wrappers for handles. Its ``BayesianQuadrature`` wrapper accepts heteroscedastic observation variances, returns cumulative integral covariance matrices, and produces seeded joint samples. ``train`` infers the integration box from the training extrema. ``train_bounded`` and ``train_bounded_1d`` store an explicit box and use it when fitting length scales. One-dimensional convenience methods avoid row-major shape arguments: .. code:: rust use gpridge_sys::safe::{BayesianQuadrature, BqHyperMode, BqKernel}; fn main() -> Result<(), gpridge_sys::safe::Error> { let mut bq = BayesianQuadrature::new_1d( BqKernel::SquaredExponential, BqHyperMode::Marginalized, )?; bq.train_1d( &[0.0, 0.25, 0.5, 0.75, 1.0], &[0.0, 0.1875, 0.25, 0.1875, 0.0], &[0.0, 1e-6, 2e-6, 1e-6, 0.0], )?; let cumulative = bq.integrate_cumulative_1d(0.0, &[0.25, 0.75, 1.0])?; let draws = bq.sample_cumulative_1d(0.0, &[0.25, 0.75, 1.0], 128, 0x5eed)?; println!("{} {}", cumulative.mean.len(), draws.len()); Ok(()) } Build and test -------------- The build needs Cargo, libclang and an installed library. With ``PKG_CONFIG_PATH`` set to the directory that holds ``gpridge.pc``, run from the checkout: .. code:: bash cargo test --locked --manifest-path rust/gpridge-sys/Cargo.toml To use a build tree or a prefix without pkg-config, set the two variables: .. code:: bash export GPRIDGE_LIBDIR=/path/to/installation/lib export GPRIDGE_INCLUDE=/path/to/installation/include cargo test --locked --manifest-path rust/gpridge-sys/Cargo.toml ``tests/smoke.rs`` checks that ``gpr_version()`` matches the crate version and that ``gpr_abi_check`` returns ``GPR_SUCCESS`` for the header's ABI. With ``GPRIDGE_LIBDIR`` set to a Meson build directory and ``GPRIDGE_INCLUDE`` set to the checkout, Cargo built the crate and reported ``version_matches_the_loaded_library ... ok`` (1 passed). The ``rust-sys`` job of ``.github/workflows/native-registries.yml`` runs ``cargo test --locked`` against an installed prefix. Example ------- A program that depends on the crate by path. ``Cargo.toml``: .. code:: toml [dependencies] gpridge-sys = { path = "/path/to/gpridge/rust/gpridge-sys" } ``src/main.rs`` checks the ABI, evaluates the Muller-Brown potential at a point near its minimum, then passes a null pointer to show the error path: .. code:: rust use std::ffi::CStr; use std::ptr; use gpridge_sys as gpr; fn text(p: *const std::os::raw::c_char) -> String { unsafe { CStr::from_ptr(p).to_string_lossy().into_owned() } } fn main() { unsafe { let status = gpr::gpr_abi_check(gpr::GPR_C_ABI_MAJOR as i32, gpr::GPR_C_ABI_MINOR as i32); assert_eq!(status, 0, "ABI mismatch: {}", text(gpr::gpr_last_error_message())); println!("gpridge {}", text(gpr::gpr_version())); let xy = [-0.558224_f64, 1.441726]; let mut energy = 0.0_f64; let mut gradient = [0.0_f64; 2]; let status = gpr::gpr_potential_muller_brown(xy.as_ptr(), &mut energy, gradient.as_mut_ptr()); assert_eq!(status, 0); println!("E = {energy:.3}, |g| = {:.3}", gradient[0].hypot(gradient[1])); let status = gpr::gpr_potential_muller_brown(ptr::null(), &mut energy, gradient.as_mut_ptr()); println!("{} {}", text(gpr::gpr_status_name(status)), text(gpr::gpr_last_error_message())); } } :: gpridge X.Y.Z E = -146.700, |g| = 0.002 GPR_ERR_INVALID_ARG gpr_potential_muller_brown: invalid argument (a null pointer, a non-positive size, or a value out of range) The output is from ``cargo run`` against a Meson build directory (``GPRIDGE_LIBDIR`` and ``GPRIDGE_INCLUDE`` set as above), with the version number replaced by ``X.Y.Z``. The documentation build does not run Rust blocks. Ownership and errors -------------------- The crate root is unsafe and raw. Rust callers using it supply valid buffers of the sizes the :doc:`C API ` specifies and release each handle with its matching destroy function. Check a constructor's result for null and a status for ``GPR_SUCCESS`` before using an output. The ``safe`` module performs those checks and releases its owned handles on drop. ``gpr_last_error_message()`` and ``gpr_last_status()`` describe the last failure on the calling thread, so read them before another C call changes that state. Use each handle from one thread at a time and finish the process-wide runtime configuration before starting a search.