From 9040d6ad66e6447fb68a35d975b24a843d690403 Mon Sep 17 00:00:00 2001 From: Jonathan Pallant Date: Fri, 18 Oct 2024 15:43:00 +0100 Subject: [PATCH 1/2] Add safe externs. --- training-slides/src/ffi.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/training-slides/src/ffi.md b/training-slides/src/ffi.md index e581a16..d271c69 100644 --- a/training-slides/src/ffi.md +++ b/training-slides/src/ffi.md @@ -233,6 +233,35 @@ Note: You cannot do `extern "C" fn some_function();` with no function body - you must use the block. +## Changes in Rust 1.82 + +You can now mark external functions as *safe*: + +```rust +unsafe extern "C" { + // This function is basically impossible to call wrong, so let's mark it safe + safe fn do_stuff(x: i32) -> i32; +} + +fn main() { + dbg!(do_stuff(3)); +} + +#[unsafe(export_name = "do_stuff")] +extern "C" fn my_do_stuff(x: i32) -> i32 { + x + 1 +} +``` + +Note: + +You can only mark an extern function as `safe` within an `unsafe extern` block. + +Also note that in Rust 1.82, `export_name` became an unsafe attribute, along +with `no_mangle` and `link_section`. The old form is still allowed in Edition +2021 and earlier (for backwards compatibility), but you will have to use the new +syntax in Edition 2024. + ## Primitive types Some C types have direct Rust equivalents. From bc78bbddb37d3471ef3ea5f310cb73868d387d4e Mon Sep 17 00:00:00 2001 From: Jonathan Pallant Date: Fri, 18 Oct 2024 15:43:19 +0100 Subject: [PATCH 2/2] More notes in FFI primitive types Also shorten the text so it fits better --- training-slides/src/ffi.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/training-slides/src/ffi.md b/training-slides/src/ffi.md index d271c69..2fc6b4d 100644 --- a/training-slides/src/ffi.md +++ b/training-slides/src/ffi.md @@ -264,10 +264,7 @@ syntax in Edition 2024. ## Primitive types -Some C types have direct Rust equivalents. - -The [`core::ffi`](https://doc.rust-lang.org/stable/core/ffi/index.html) module -also defines a bunch of useful types and aliases. +Some C types have direct Rust equivalents. See also [`core::ffi`](https://doc.rust-lang.org/stable/core/ffi/index.html). | C | Rust | | --------------- | -------------------------- | @@ -284,6 +281,10 @@ On some systems, a C `char` is not 8 bits in size. Rust does not support those platforms, and likely never will. Rust does support platforms where `int` is only 16-bits in size. +If `T: ?Sized`, then `Box` may be larger than a single pointer as it will +also need to hold the length information. That means it is no longer the same +size and layout as `T*`. + ## Calling this ```rust ignore []