Skip to content

Conversation

Neutron3529
Copy link

2 macros are provided to ensure unsafe rust program will be called safely.

One of them is ffi, which allow calling ffi in safe rust:

// create safe printf function
ffi!{
    extern "C" fn printf(fmt: *const i8)->i32
}
fn main(){
    printf(c"Calling FFI in a safe way:)\n".as_ptr())
}

Another is a safe macro, which converts the unsafe function into the safe version. But sadly, you have to provide the signature about the old unsafe function (to ensure some safetyness..)

use r#unsafe::r#unsafe;
safe!{
    fn r#unsafe()->i32 := very_safe
}
fn main(){
    // then you can use `very_safe()` to call `r#unsafe::r#unsafe` safely:)
}

@workingjubilee
Copy link

workingjubilee commented Sep 14, 2024

Needs a test which demonstrates that this works even if you deny(unsafe_code) in the crate it is expanded in (otherwise, it's not actually making the call safe!)

@workingjubilee
Copy link

Oh it does have one, neat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants