Skip to content

Commit e26cd70

Browse files
authored
Fix build warning due to unused return value (#80)
* Fix build warning due to unused return value * Fix build warning about accessing static global
1 parent e32d69a commit e26cd70

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

freertos-rust/src/hooks.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@ use crate::base::*;
22
use crate::prelude::v1::String;
33
use crate::utils::*;
44

5+
use core::cell::OnceCell;
6+
57
type Callback = fn();
68

79
pub struct FreeRtosHooks {
8-
on_assert: Callback,
10+
on_assert: OnceCell<Callback>,
911
}
1012

1113
impl FreeRtosHooks {
12-
pub fn set_on_assert(&mut self, c: Callback) {
13-
self.on_assert = c;
14+
pub fn set_on_assert(&mut self, c: Callback) -> Result<(), Callback> {
15+
self.on_assert.set(c)
1416
}
1517

1618
fn do_on_assert(&self) {
17-
(self.on_assert)();
19+
if let Some (cb) = self.on_assert.get() {
20+
cb()
21+
}
1822
}
1923
}
2024

21-
// TODO: It's unsafe to use, we should build some safe wrapper around
22-
pub static mut FREERTOS_HOOKS: FreeRtosHooks = FreeRtosHooks { on_assert: || {} };
25+
// SAFETY: must only be set before the scheduler starts and accessed after the
26+
// kernel has asserted, both being single threaded situations.
27+
unsafe impl Sync for FreeRtosHooks {}
28+
29+
pub static FREERTOS_HOOKS: FreeRtosHooks = FreeRtosHooks { on_assert: OnceCell::new() };
2330

2431
#[allow(unused_doc_comments)]
2532
#[no_mangle]
@@ -29,9 +36,7 @@ pub extern "C" fn vAssertCalled(file_name_ptr: FreeRtosCharPtr, line: FreeRtosUB
2936
file_name = str_from_c_string(file_name_ptr).unwrap();
3037
}
3138

32-
unsafe {
33-
FREERTOS_HOOKS.do_on_assert();
34-
}
39+
FREERTOS_HOOKS.do_on_assert();
3540

3641
// we can't print without std yet.
3742
// TODO: make the macro work for debug UART? Or use Panic here?

freertos-rust/src/timers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Timer {
121121
if let Ok(callback_ptr) = timer.get_id() {
122122
let b = Box::from_raw(callback_ptr as *mut Box<dyn Fn(Timer)>);
123123
b(timer);
124-
Box::into_raw(b);
124+
let _ = Box::into_raw(b);
125125
}
126126
}
127127
}

0 commit comments

Comments
 (0)