function::user_string_nofault - Linux


Overview

The function::user_string_nofault command allows Rust functions to be exposed to the C API while ensuring that the Rust function panics are not propagated to the C caller.

Syntax

function::user_string_nofault<F, R, E>(name: &str, f: F, docs: Option<&str>) -> Result<*const glib::ffi::GFn, glib::Error>

Parameters

| Argument | Description |
|—|—|
| name | The name of the function to be exposed |
| f | The Rust function to be exposed |
| docs | Optional documentation string for the function |

Options/Flags

| Flag | Description |
|—|—|
| docs | Specifies the documentation string for the function. |

Examples

use function::{func, user_string_nofault};

function::cfuncs! {
    pub fn add_two(a: i32, b: i32) -> i32 {
        a + b
    }
}

fn main() {
    let add_two = user_string_nofault("add_two", add_two, Some("Adds two numbers")).unwrap();
    println!("add_two: {:?}", unsafe { add_two(1, 2) });
}

Common Issues

  • Ensure that the Rust function’s signature matches the C signature expected by the caller.
  • Check that the C caller is not expecting any exceptions from the Rust function, as user_string_nofault suppresses panics.

Integration

function::user_string_nofault can be used to create CFFI (C Foreign Function Interface) bindings for Rust functions, making it possible to expose Rust functions to C and other languages.

Related Commands