Skip to content

Commit 60ed8f3

Browse files
committed
Update to 2024 edition
1 parent 8f5c732 commit 60ed8f3

File tree

7 files changed

+28
-28
lines changed

7 files changed

+28
-28
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
merge_group:
55

66
env:
7-
MDBOOK_VERSION: 0.4.40
7+
MDBOOK_VERSION: 0.4.45
88

99
jobs:
1010
test:

book.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ git-repository-url = "https://github.com/rust-lang/nomicon"
3232
"./arc.html" = "./arc-mutex/arc.html"
3333

3434
[rust]
35-
edition = "2021"
35+
edition = "2024"

src/beneath-std.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use core::ffi::{c_char, c_int};
5353
use core::panic::PanicInfo;
5454

5555
// Entry point for this program.
56-
#[no_mangle] // ensure that this symbol is included in the output as `main`
56+
#[unsafe(no_mangle)] // ensure that this symbol is included in the output as `main`
5757
extern "C" fn main(_argc: c_int, _argv: *const *const c_char) -> c_int {
5858
0
5959
}

src/ffi.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ compile if snappy is installed:
3131
use libc::size_t;
3232
3333
#[link(name = "snappy")]
34-
extern {
34+
unsafe extern "C" {
3535
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
3636
}
3737
@@ -64,7 +64,7 @@ The `extern` block can be extended to cover the entire snappy API:
6464
use libc::{c_int, size_t};
6565
6666
#[link(name = "snappy")]
67-
extern {
67+
unsafe extern {
6868
fn snappy_compress(input: *const u8,
6969
input_length: size_t,
7070
compressed: *mut u8,
@@ -251,7 +251,7 @@ First, we assume you have a lib crate named as `rust_from_c`.
251251
`lib.rs` should have Rust code as following:
252252

253253
```rust
254-
#[no_mangle]
254+
#[unsafe(no_mangle)]
255255
pub extern "C" fn hello_from_rust() {
256256
println!("Hello from Rust!");
257257
}
@@ -331,7 +331,7 @@ extern fn callback(a: i32) {
331331
}
332332
333333
#[link(name = "extlib")]
334-
extern {
334+
unsafe extern {
335335
fn register_callback(cb: extern fn(i32)) -> i32;
336336
fn trigger_callback();
337337
}
@@ -383,7 +383,7 @@ struct RustObject {
383383
// Other members...
384384
}
385385
386-
extern "C" fn callback(target: *mut RustObject, a: i32) {
386+
unsafe extern "C" fn callback(target: *mut RustObject, a: i32) {
387387
println!("I'm called from C with value {0}", a);
388388
unsafe {
389389
// Update the value in RustObject with the value received from the callback:
@@ -392,9 +392,9 @@ extern "C" fn callback(target: *mut RustObject, a: i32) {
392392
}
393393
394394
#[link(name = "extlib")]
395-
extern {
395+
unsafe extern {
396396
fn register_callback(target: *mut RustObject,
397-
cb: extern fn(*mut RustObject, i32)) -> i32;
397+
cb: unsafe extern fn(*mut RustObject, i32)) -> i32;
398398
fn trigger_callback();
399399
}
400400
@@ -523,7 +523,7 @@ blocks with the `static` keyword:
523523
<!-- ignore: requires libc crate -->
524524
```rust,ignore
525525
#[link(name = "readline")]
526-
extern {
526+
unsafe extern {
527527
static rl_readline_version: libc::c_int;
528528
}
529529
@@ -543,7 +543,7 @@ use std::ffi::CString;
543543
use std::ptr;
544544
545545
#[link(name = "readline")]
546-
extern {
546+
unsafe extern {
547547
static mut rl_prompt: *const libc::c_char;
548548
}
549549
@@ -573,7 +573,7 @@ conventions. Rust provides a way to tell the compiler which convention to use:
573573
#[cfg(all(target_os = "win32", target_arch = "x86"))]
574574
#[link(name = "kernel32")]
575575
#[allow(non_snake_case)]
576-
extern "stdcall" {
576+
unsafe extern "stdcall" {
577577
fn SetEnvironmentVariableA(n: *const u8, v: *const u8) -> libc::c_int;
578578
}
579579
# fn main() { }
@@ -635,7 +635,7 @@ In C, functions can be 'variadic', meaning they accept a variable number of argu
635635
be achieved in Rust by specifying `...` within the argument list of a foreign function declaration:
636636

637637
```no_run
638-
extern {
638+
unsafe extern {
639639
fn foo(x: i32, ...);
640640
}
641641
@@ -685,7 +685,7 @@ we have function pointers flying across the FFI boundary in both directions.
685685
use libc::c_int;
686686
687687
# #[cfg(hidden)]
688-
extern "C" {
688+
unsafe extern "C" {
689689
/// Registers the callback.
690690
fn register(cb: Option<extern "C" fn(Option<extern "C" fn(c_int) -> c_int>, c_int) -> c_int>);
691691
}
@@ -750,8 +750,8 @@ mechanisms (notably C++'s `try`/`catch`).
750750
751751
<!-- ignore: using unstable feature -->
752752
```rust,ignore
753-
#[no_mangle]
754-
extern "C-unwind" fn example() {
753+
#[unsafe(no_mangle)]
754+
unsafe extern "C-unwind" fn example() {
755755
panic!("Uh oh");
756756
}
757757
```
@@ -780,13 +780,13 @@ If the C++ frames have objects, their destructors will be called.
780780
<!-- ignore: using unstable feature -->
781781
```rust,ignore
782782
#[link(...)]
783-
extern "C-unwind" {
783+
unsafe extern "C-unwind" {
784784
// A C++ function that may throw an exception
785785
fn may_throw();
786786
}
787787
788-
#[no_mangle]
789-
extern "C-unwind" fn rust_passthrough() {
788+
#[unsafe(no_mangle)]
789+
unsafe extern "C-unwind" fn rust_passthrough() {
790790
let b = Box::new(5);
791791
unsafe { may_throw(); }
792792
println!("{:?}", &b);
@@ -816,7 +816,7 @@ will be printed.
816816
### `panic` can be stopped at an ABI boundary
817817

818818
```rust
819-
#[no_mangle]
819+
#[unsafe(no_mangle)]
820820
extern "C" fn assert_nonzero(input: u32) {
821821
assert!(input != 0)
822822
}
@@ -833,7 +833,7 @@ process if it panics, you must use [`catch_unwind`]:
833833
```rust
834834
use std::panic::catch_unwind;
835835

836-
#[no_mangle]
836+
#[unsafe(no_mangle)]
837837
pub extern "C" fn oh_no() -> i32 {
838838
let result = catch_unwind(|| {
839839
panic!("Oops!");
@@ -867,7 +867,7 @@ We can represent this in Rust with the `c_void` type:
867867
868868
<!-- ignore: requires libc crate -->
869869
```rust,ignore
870-
extern "C" {
870+
unsafe extern "C" {
871871
pub fn foo(arg: *mut libc::c_void);
872872
pub fn bar(arg: *mut libc::c_void);
873873
}
@@ -902,7 +902,7 @@ pub struct Bar {
902902
core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
903903
}
904904
905-
extern "C" {
905+
unsafe extern "C" {
906906
pub fn foo(arg: *mut Foo);
907907
pub fn bar(arg: *mut Bar);
908908
}

src/intro.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Topics that are within the scope of this book include: the meaning of (un)safety
3939

4040
The Rustonomicon is not a place to exhaustively describe the semantics and guarantees of every single API in the standard library, nor is it a place to exhaustively describe every feature of Rust.
4141

42-
Unless otherwise noted, Rust code in this book uses the Rust 2021 edition.
42+
Unless otherwise noted, Rust code in this book uses the Rust 2024 edition.
4343

4444
[trpl]: ../book/index.html
4545
[ref]: ../reference/index.html

src/other-reprs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ and it will become a hard error.
145145
`repr(packed)/repr(packed(n))` is not to be used lightly. Unless you have
146146
extreme requirements, this should not be used.
147147

148-
This repr is a modifier on `repr(C)` and `repr(Rust)`. For FFI compatibilty
148+
This repr is a modifier on `repr(C)` and `repr(Rust)`. For FFI compatibility
149149
you most likely always want to be explicit: `repr(C, packed)`.
150150

151151
## repr(align(n))

src/send-and-sync.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ to the heap.
8989
# pub use ::std::os::raw::{c_int, c_void};
9090
# #[allow(non_camel_case_types)]
9191
# pub type size_t = usize;
92-
# extern "C" { pub fn posix_memalign(memptr: *mut *mut c_void, align: size_t, size: size_t) -> c_int; }
92+
# unsafe extern "C" { pub fn posix_memalign(memptr: *mut *mut c_void, align: size_t, size: size_t) -> c_int; }
9393
# }
9494
use std::{
9595
mem::{align_of, size_of},
@@ -225,7 +225,7 @@ allocation done on another thread. We can check this is true in the docs for
225225
# struct Carton<T>(std::ptr::NonNull<T>);
226226
# mod libc {
227227
# pub use ::std::os::raw::c_void;
228-
# extern "C" { pub fn free(p: *mut c_void); }
228+
# unsafe extern "C" { pub fn free(p: *mut c_void); }
229229
# }
230230
impl<T> Drop for Carton<T> {
231231
fn drop(&mut self) {

0 commit comments

Comments
 (0)