1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-12-22 22:51:12 +02:00

Updates for Rust 1.82 (#2449)

Rust 1.82 adds `&raw` expressions, and marks some attributes as unsafe.
This commit is contained in:
Andrew Walbran
2024-11-01 07:39:56 +00:00
committed by GitHub
parent 2bba470415
commit f8882190f3
24 changed files with 100 additions and 72 deletions

View File

@@ -20,7 +20,8 @@ use jni::sys::jstring;
use jni::JNIEnv;
/// HelloWorld::hello method implementation.
#[no_mangle]
// SAFETY: There is no other global function of this name.
#[unsafe(no_mangle)]
pub extern "system" fn Java_HelloWorld_hello(
mut env: JNIEnv,
_class: JClass,

View File

@@ -6,14 +6,13 @@ Similarly, you can export Rust functions and call them from C.
You can do it by hand if you want:
```rust
extern "C" {
fn abs(x: i32) -> i32;
unsafe extern "C" {
safe fn abs(x: i32) -> i32;
}
fn main() {
let x = -42;
// SAFETY: `abs` doesn't have any safety requirements.
let abs_x = unsafe { abs(x) };
let abs_x = abs(x);
println!("{x}, {abs_x}");
}
```

View File

@@ -3,14 +3,13 @@
We can declare external functions by hand:
```rust
extern "C" {
fn abs(x: i32) -> i32;
unsafe extern "C" {
safe fn abs(x: i32) -> i32;
}
fn main() {
let x = -42;
// SAFETY: `abs` doesn't have any safety requirements.
let abs_x = unsafe { abs(x) };
let abs_x = abs(x);
println!("{x}, {abs_x}");
}
```

View File

@@ -42,8 +42,8 @@ Build, push, and run the binary on your device:
<details>
`#[no_mangle]` disables Rust's usual name mangling, so the exported symbol will
just be the name of the function. You can also use
`#[export_name = "some_name"]` to specify whatever name you want.
`#[unsafe(no_mangle)]` disables Rust's usual name mangling, so the exported
symbol will just be the name of the function. You can also use
`#[unsafe(export_name = "some_name")]` to specify whatever name you want.
</details>

View File

@@ -19,7 +19,8 @@
use std::os::raw::c_int;
/// Analyze the numbers.
#[no_mangle]
// SAFETY: There is no other global function of this name.
#[unsafe(no_mangle)]
pub extern "C" fn analyze_numbers(x: c_int, y: c_int) {
if x < y {
println!("x ({x}) is smallest!");