docs(refs): Add unsafe to extern while using build scripts in Cargo Book (#15294)

### What does this PR try to resolve?

Since `rust 1.85.0`, It is no longer sufficient to use the `unsafe`
keyword only at the call site. It must also be applied to the `extern`
statement, hence, Cargo Book must be updated

- closes https://github.com/rust-lang/cargo/issues/15289

and also update some more related content

### How should we test and review this PR?

Run `mdbook build`. The `unsafe extern` should be seen at
`cargo/reference/build-script-examples.html#linking-to-system-libraries`
, `cargo/reference/build-script-examples.html#building-a-native-library`
, `cargo/reference/semver.html#repr-c-shuffle` ,
`cargo/reference/semver.html#repr-c-remove` and
`cargo/reference/semver.html#repr-transparent-remove`

### Additional information
This commit is contained in:
Ed Page 2025-03-11 01:17:36 +00:00 committed by GitHub
commit 3897c7cb0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 5 deletions

View File

@ -252,7 +252,7 @@ void hello() {
// Note the lack of the `#[link]` attribute. Were delegating the responsibility // Note the lack of the `#[link]` attribute. Were delegating the responsibility
// of selecting what to link over to the build script rather than hard-coding // of selecting what to link over to the build script rather than hard-coding
// it in the source file. // it in the source file.
extern { fn hello(); } unsafe extern { fn hello(); }
fn main() { fn main() {
unsafe { hello(); } unsafe { hello(); }
@ -327,7 +327,7 @@ Let's round out the example with a basic FFI binding:
use std::os::raw::{c_uint, c_ulong}; use std::os::raw::{c_uint, c_ulong};
extern "C" { unsafe extern "C" {
pub fn crc32(crc: c_ulong, buf: *const u8, len: c_uint) -> c_ulong; pub fn crc32(crc: c_ulong, buf: *const u8, len: c_uint) -> c_ulong;
} }

View File

@ -766,7 +766,7 @@ pub struct SpecificLayout {
// Example usage that will break. // Example usage that will break.
use updated_crate::SpecificLayout; use updated_crate::SpecificLayout;
extern "C" { unsafe extern "C" {
// This C function is assuming a specific layout defined in a C header. // This C function is assuming a specific layout defined in a C header.
fn c_fn_get_b(x: &SpecificLayout) -> u32; fn c_fn_get_b(x: &SpecificLayout) -> u32;
} }
@ -820,7 +820,7 @@ pub struct SpecificLayout {
// Example usage that will break. // Example usage that will break.
use updated_crate::SpecificLayout; use updated_crate::SpecificLayout;
extern "C" { unsafe extern "C" {
// This C function is assuming a specific layout defined in a C header. // This C function is assuming a specific layout defined in a C header.
fn c_fn_get_b(x: &SpecificLayout) -> u32; // Error: is not FFI-safe fn c_fn_get_b(x: &SpecificLayout) -> u32; // Error: is not FFI-safe
} }
@ -941,7 +941,7 @@ pub struct Transparent<T>(T);
#![deny(improper_ctypes)] #![deny(improper_ctypes)]
use updated_crate::Transparent; use updated_crate::Transparent;
extern "C" { unsafe extern "C" {
fn c_fn() -> Transparent<f64>; // Error: is not FFI-safe fn c_fn() -> Transparent<f64>; // Error: is not FFI-safe
} }