Skip to content

Commit 10b8831

Browse files
committed
Update all code to be edition 2024 compatible.
1 parent 51620b3 commit 10b8831

15 files changed

Lines changed: 246 additions & 216 deletions

File tree

cortex-m-rt/examples/device.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Manually create the interrupts portion of the vector table
22
3-
#![deny(unsafe_code)]
43
#![deny(warnings)]
54
#![no_main]
65
#![no_std]
@@ -22,14 +21,14 @@ pub union Vector {
2221
reserved: usize,
2322
}
2423

25-
extern "C" {
24+
unsafe extern "C" {
2625
fn WWDG();
2726
fn PVD();
2827
}
2928

3029
#[allow(unsafe_code)]
31-
#[link_section = ".vector_table.interrupts"]
32-
#[no_mangle]
30+
#[unsafe(link_section = ".vector_table.interrupts")]
31+
#[unsafe(no_mangle)]
3332
pub static __INTERRUPTS: [Vector; 3] = [
3433
Vector { handler: WWDG },
3534
Vector { reserved: 0 },

cortex-m-rt/examples/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
extern crate cortex_m_rt as rt;
88
extern crate panic_halt;
99

10-
#[no_mangle]
10+
#[unsafe(no_mangle)]
1111
pub unsafe extern "C" fn main() -> ! {
1212
loop {}
1313
}

cortex-m-rt/examples/unsafety.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ use cortex_m_rt::{entry, exception, ExceptionFrame};
1111

1212
#[entry]
1313
unsafe fn main() -> ! {
14-
foo();
14+
unsafe { foo() };
1515

1616
loop {}
1717
}
1818

1919
#[exception]
2020
unsafe fn DefaultHandler(_irqn: i16) {
21-
foo();
21+
unsafe { foo() };
2222
}
2323

2424
#[exception]
2525
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
26-
foo();
26+
unsafe { foo() };
2727

2828
loop {}
2929
}
3030

3131
#[exception]
3232
unsafe fn SysTick() {
33-
foo();
33+
unsafe { foo() };
3434
}
3535

3636
unsafe fn foo() {}

cortex-m-rt/src/lib.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,13 @@
361361
//! reserved: usize,
362362
//! }
363363
//!
364-
//! extern "C" {
364+
//! unsafe extern "C" {
365365
//! fn Foo();
366366
//! fn Bar();
367367
//! }
368368
//!
369-
//! #[link_section = ".vector_table.interrupts"]
370-
//! #[no_mangle]
369+
//! #[unsafe(link_section = ".vector_table.interrupts")]
370+
//! #[unsafe(no_mangle)]
371371
//! pub static __INTERRUPTS: [Vector; 5] = [
372372
//! // 0-1: Reserved
373373
//! Vector { reserved: 0 },
@@ -440,14 +440,14 @@
440440
//!
441441
//! [`MaybeUninit`]: https://doc.rust-lang.org/core/mem/union.MaybeUninit.html
442442
//!
443-
//! ```no_run,edition2018
443+
//! ```no_run
444444
//! # extern crate core;
445445
//! use core::mem::MaybeUninit;
446446
//!
447447
//! const STACK_SIZE: usize = 8 * 1024;
448448
//! const NTHREADS: usize = 4;
449449
//!
450-
//! #[link_section = ".uninit.STACKS"]
450+
//! #[unsafe(link_section = ".uninit.STACKS")]
451451
//! static mut STACKS: MaybeUninit<[[u8; STACK_SIZE]; NTHREADS]> = MaybeUninit::uninit();
452452
//! ```
453453
//!
@@ -483,10 +483,10 @@
483483
//!
484484
//! You can then use something like this to place a variable into this specific section of memory:
485485
//!
486-
//! ```no_run,edition2018
486+
//! ```no_run
487487
//! # extern crate core;
488488
//! # use core::mem::MaybeUninit;
489-
//! #[link_section=".ccmram.BUFFERS"]
489+
//! #[unsafe(link_section=".ccmram.BUFFERS")]
490490
//! static mut BUF: MaybeUninit<[u8; 1024]> = MaybeUninit::uninit();
491491
//! ```
492492
//!
@@ -937,7 +937,7 @@ pub use macros::pre_init;
937937
// two copies of cortex-m-rt together, linking will fail. We also declare a links key in
938938
// Cargo.toml which is the more modern way to solve the same problem, but we have to keep
939939
// __ONCE__ around to prevent linking with versions before the links key was added.
940-
#[export_name = "error: cortex-m-rt appears more than once in the dependency graph"]
940+
#[unsafe(export_name = "error: cortex-m-rt appears more than once in the dependency graph")]
941941
#[doc(hidden)]
942942
pub static __ONCE__: () = ();
943943

@@ -1120,7 +1120,7 @@ impl fmt::Debug for ExceptionFrame {
11201120
/// The returned pointer is guaranteed to be 4-byte aligned.
11211121
#[inline]
11221122
pub fn heap_start() -> *mut u32 {
1123-
extern "C" {
1123+
unsafe extern "C" {
11241124
static mut __sheap: u32;
11251125
}
11261126

@@ -1132,27 +1132,27 @@ pub fn heap_start() -> *mut u32 {
11321132

11331133
// Entry point is Reset.
11341134
#[doc(hidden)]
1135-
#[cfg_attr(cortex_m, link_section = ".vector_table.reset_vector")]
1136-
#[no_mangle]
1135+
#[cfg_attr(cortex_m, unsafe(link_section = ".vector_table.reset_vector"))]
1136+
#[unsafe(no_mangle)]
11371137
pub static __RESET_VECTOR: unsafe extern "C" fn() -> ! = Reset;
11381138

11391139
#[doc(hidden)]
1140-
#[cfg_attr(cortex_m, link_section = ".HardFault.default")]
1141-
#[no_mangle]
1140+
#[cfg_attr(cortex_m, unsafe(link_section = ".HardFault.default"))]
1141+
#[unsafe(no_mangle)]
11421142
pub unsafe extern "C" fn HardFault_() -> ! {
11431143
#[allow(clippy::empty_loop)]
11441144
loop {}
11451145
}
11461146

11471147
#[doc(hidden)]
1148-
#[no_mangle]
1148+
#[unsafe(no_mangle)]
11491149
pub unsafe extern "C" fn DefaultHandler_() -> ! {
11501150
#[allow(clippy::empty_loop)]
11511151
loop {}
11521152
}
11531153

11541154
#[doc(hidden)]
1155-
#[no_mangle]
1155+
#[unsafe(no_mangle)]
11561156
pub unsafe extern "C" fn DefaultPreInit() {}
11571157

11581158
/* Exceptions */
@@ -1187,7 +1187,7 @@ pub enum Exception {
11871187
#[doc(hidden)]
11881188
pub use self::Exception as exception;
11891189

1190-
extern "C" {
1190+
unsafe extern "C" {
11911191
fn Reset() -> !;
11921192

11931193
fn NonMaskableInt();
@@ -1224,8 +1224,8 @@ pub union Vector {
12241224
}
12251225

12261226
#[doc(hidden)]
1227-
#[cfg_attr(cortex_m, link_section = ".vector_table.exceptions")]
1228-
#[no_mangle]
1227+
#[cfg_attr(cortex_m, unsafe(link_section = ".vector_table.exceptions"))]
1228+
#[unsafe(no_mangle)]
12291229
pub static __EXCEPTIONS: [Vector; 14] = [
12301230
// Exception 2: Non Maskable Interrupt.
12311231
Vector {
@@ -1284,10 +1284,10 @@ pub static __EXCEPTIONS: [Vector; 14] = [
12841284
// to the default handler
12851285
#[cfg(all(any(not(feature = "device"), test), not(armv6m), not(armv8m_main)))]
12861286
#[doc(hidden)]
1287-
#[cfg_attr(cortex_m, link_section = ".vector_table.interrupts")]
1288-
#[no_mangle]
1287+
#[cfg_attr(cortex_m, unsafe(link_section = ".vector_table.interrupts"))]
1288+
#[unsafe(no_mangle)]
12891289
pub static __INTERRUPTS: [unsafe extern "C" fn(); 240] = [{
1290-
extern "C" {
1290+
unsafe extern "C" {
12911291
fn DefaultHandler();
12921292
}
12931293

@@ -1297,10 +1297,10 @@ pub static __INTERRUPTS: [unsafe extern "C" fn(); 240] = [{
12971297
// ARMv8-M Mainline can have up to 480 device specific interrupts
12981298
#[cfg(all(not(feature = "device"), armv8m_main))]
12991299
#[doc(hidden)]
1300-
#[cfg_attr(cortex_m, link_section = ".vector_table.interrupts")]
1301-
#[no_mangle]
1300+
#[cfg_attr(cortex_m, unsafe(link_section = ".vector_table.interrupts"))]
1301+
#[unsafe(no_mangle)]
13021302
pub static __INTERRUPTS: [unsafe extern "C" fn(); 480] = [{
1303-
extern "C" {
1303+
unsafe extern "C" {
13041304
fn DefaultHandler();
13051305
}
13061306

@@ -1310,10 +1310,10 @@ pub static __INTERRUPTS: [unsafe extern "C" fn(); 480] = [{
13101310
// ARMv6-M can only have a maximum of 32 device specific interrupts
13111311
#[cfg(all(not(feature = "device"), armv6m))]
13121312
#[doc(hidden)]
1313-
#[link_section = ".vector_table.interrupts"]
1314-
#[no_mangle]
1313+
#[unsafe(link_section = ".vector_table.interrupts")]
1314+
#[unsafe(no_mangle)]
13151315
pub static __INTERRUPTS: [unsafe extern "C" fn(); 32] = [{
1316-
extern "C" {
1316+
unsafe extern "C" {
13171317
fn DefaultHandler();
13181318
}
13191319

cortex-m-semihosting/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub mod nr;
193193
/// [semihosting operation]: https://developer.arm.com/documentation/dui0471/i/semihosting/semihosting-operations?lang=en
194194
#[inline(always)]
195195
pub unsafe fn syscall<T>(nr: usize, arg: &T) -> usize {
196-
syscall1(nr, arg as *const T as usize)
196+
unsafe { syscall1(nr, arg as *const T as usize) }
197197
}
198198

199199
/// Performs a semihosting operation, takes one integer as an argument
@@ -209,7 +209,9 @@ pub unsafe fn syscall1(_nr: usize, _arg: usize) -> usize {
209209
use core::arch::asm;
210210
let mut nr = _nr as u32;
211211
let arg = _arg as u32;
212-
asm!("bkpt #0xab", inout("r0") nr, in("r1") arg, options(nostack, preserves_flags));
212+
unsafe {
213+
asm!("bkpt #0xab", inout("r0") nr, in("r1") arg, options(nostack, preserves_flags));
214+
}
213215
nr as usize
214216
}
215217
#[cfg(all(thumb, feature = "no-semihosting"))]

cortex-m/src/asm.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ pub unsafe fn bootstrap(msp: *const u32, rv: *const u32) -> ! {
293293
/// a valid reset vector as the second word.
294294
#[inline]
295295
pub unsafe fn bootload(vector_table: *const u32) -> ! {
296-
let msp = core::ptr::read_volatile(vector_table);
297-
let rv = core::ptr::read_volatile(vector_table.offset(1));
298-
bootstrap(msp as *const u32, rv as *const u32);
296+
unsafe {
297+
let msp = core::ptr::read_volatile(vector_table);
298+
let rv = core::ptr::read_volatile(vector_table.offset(1));
299+
bootstrap(msp as *const u32, rv as *const u32);
300+
}
299301
}

cortex-m/src/call_asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ macro_rules! call_asm {
1212

1313
#[cfg(not(feature = "inline-asm"))]
1414
() => {
15-
extern "C" {
15+
unsafe extern "C" {
1616
fn $func($($args: $tys),*) $(-> $ret)?;
1717
}
1818

cortex-m/src/critical_section.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ unsafe impl Impl for SingleCoreCriticalSection {
1919

2020
unsafe fn release(restore_state: RawRestoreState) {
2121
// NOTE: Fence guarantees are provided by primask::write_raw(), which performs a `compiler_fence(SeqCst)`.
22-
primask::write_raw(restore_state);
22+
unsafe { primask::write_raw(restore_state) };
2323
}
2424
}

cortex-m/src/interrupt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ where
6565
// disable interrupts
6666
disable();
6767

68-
let r = f(unsafe { &CriticalSection::new() });
68+
let r = f(&unsafe { CriticalSection::new() });
6969

7070
unsafe {
7171
crate::register::primask::write_raw(primask);

cortex-m/src/itm.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,54 @@ use crate::peripheral::itm::Stim;
88

99
// NOTE assumes that `bytes` is 32-bit aligned
1010
unsafe fn write_words(stim: &mut Stim, bytes: &[u32]) {
11-
let mut p = bytes.as_ptr();
12-
for _ in 0..bytes.len() {
13-
while !stim.is_fifo_ready() {}
14-
stim.write_u32(ptr::read(p));
15-
p = p.offset(1);
11+
unsafe {
12+
let mut p = bytes.as_ptr();
13+
for _ in 0..bytes.len() {
14+
while !stim.is_fifo_ready() {}
15+
stim.write_u32(ptr::read(p));
16+
p = p.offset(1);
17+
}
1618
}
1719
}
1820

1921
/// Writes an aligned byte slice to the ITM.
2022
///
2123
/// `buffer` must be 4-byte aligned.
2224
unsafe fn write_aligned_impl(port: &mut Stim, buffer: &[u8]) {
23-
let len = buffer.len();
25+
unsafe {
26+
let len = buffer.len();
2427

25-
if len == 0 {
26-
return;
27-
}
28+
if len == 0 {
29+
return;
30+
}
2831

29-
let split = len & !0b11;
30-
#[allow(clippy::cast_ptr_alignment)]
31-
write_words(
32-
port,
33-
slice::from_raw_parts(buffer.as_ptr() as *const u32, split >> 2),
34-
);
32+
let split = len & !0b11;
33+
#[allow(clippy::cast_ptr_alignment)]
34+
write_words(
35+
port,
36+
slice::from_raw_parts(buffer.as_ptr() as *const u32, split >> 2),
37+
);
3538

36-
// 3 bytes or less left
37-
let mut left = len & 0b11;
38-
let mut ptr = buffer.as_ptr().add(split);
39+
// 3 bytes or less left
40+
let mut left = len & 0b11;
41+
let mut ptr = buffer.as_ptr().add(split);
3942

40-
// at least 2 bytes left
41-
if left > 1 {
42-
while !port.is_fifo_ready() {}
43+
// at least 2 bytes left
44+
if left > 1 {
45+
while !port.is_fifo_ready() {}
4346

44-
#[allow(clippy::cast_ptr_alignment)]
45-
port.write_u16(ptr::read(ptr as *const u16));
47+
#[allow(clippy::cast_ptr_alignment)]
48+
port.write_u16(ptr::read(ptr as *const u16));
4649

47-
ptr = ptr.offset(2);
48-
left -= 2;
49-
}
50+
ptr = ptr.offset(2);
51+
left -= 2;
52+
}
5053

51-
// final byte
52-
if left == 1 {
53-
while !port.is_fifo_ready() {}
54-
port.write_u8(*ptr);
54+
// final byte
55+
if left == 1 {
56+
while !port.is_fifo_ready() {}
57+
port.write_u8(*ptr);
58+
}
5559
}
5660
}
5761

0 commit comments

Comments
 (0)