Skip to content

Commit 5a67315

Browse files
author
Sven Van Asbroeck
committed
rust/samples: miscdev: eliminate unsafe block
When creating a pinned `Arc`, eliminate an `unsafe` block by using the fallible version of `Arc::pin()`. While we're here, update the `// SAFETY` proofs, which have become stale. Tested using QEMU. Signed-off-by: Sven Van Asbroeck <[email protected]>
1 parent 02138fd commit 5a67315

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

samples/rust/rust_miscdev.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,16 @@ struct SharedState {
3939

4040
impl SharedState {
4141
fn try_new() -> Result<Pin<Arc<Self>>> {
42-
// SAFETY: `state` is pinning `Arc`, which implements `Unpin`.
43-
let state = unsafe {
44-
Pin::new_unchecked(Arc::try_new(Self {
45-
// SAFETY: `condvar_init!` is called below.
46-
state_changed: CondVar::new(),
47-
// SAFETY: `mutex_init!` is called below.
48-
inner: Mutex::new(SharedStateInner { token_count: 0 }),
49-
})?)
50-
};
51-
// SAFETY: `state_changed` is pinned behind `Arc`.
42+
let state = Arc::try_pin(Self {
43+
// SAFETY: `condvar_init!` is called below.
44+
state_changed: unsafe { CondVar::new() },
45+
// SAFETY: `mutex_init!` is called below.
46+
inner: unsafe { Mutex::new(SharedStateInner { token_count: 0 }) },
47+
})?;
48+
// SAFETY: `state_changed` is pinned behind `Pin<Arc>`.
5249
let state_changed = unsafe { Pin::new_unchecked(&state.state_changed) };
5350
kernel::condvar_init!(state_changed, "SharedState::state_changed");
54-
// SAFETY: `inner` is pinned behind `Arc`.
51+
// SAFETY: `inner` is pinned behind `Pin<Arc>`.
5552
let inner = unsafe { Pin::new_unchecked(&state.inner) };
5653
kernel::mutex_init!(inner, "SharedState::inner");
5754
Ok(state)

0 commit comments

Comments
 (0)