Skip to content

Commit 1a73789

Browse files
authored
Merge pull request #289 from obviyus/change-pin
rust: make SharedState::try_new() return a pinned Arc
2 parents 45d79fc + 84da4b2 commit 1a73789

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

samples/rust/rust_miscdev.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ struct SharedState {
3838
}
3939

4040
impl SharedState {
41-
fn try_new() -> Result<Arc<Self>> {
42-
let state = Arc::try_new(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-
})?;
41+
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+
};
4851
// SAFETY: `state_changed` is pinned behind `Arc`.
4952
let state_changed = unsafe { Pin::new_unchecked(&state.state_changed) };
5053
kernel::condvar_init!(state_changed, "SharedState::state_changed");
@@ -56,11 +59,11 @@ impl SharedState {
5659
}
5760

5861
struct Token {
59-
shared: Arc<SharedState>,
62+
shared: Pin<Arc<SharedState>>,
6063
}
6164

62-
impl FileOpener<Arc<SharedState>> for Token {
63-
fn open(shared: &Arc<SharedState>) -> Result<Self::Wrapper> {
65+
impl FileOpener<Pin<Arc<SharedState>>> for Token {
66+
fn open(shared: &Pin<Arc<SharedState>>) -> Result<Self::Wrapper> {
6467
Ok(Box::try_new(Self {
6568
shared: shared.clone(),
6669
})?)
@@ -122,7 +125,7 @@ impl FileOperations for Token {
122125
}
123126

124127
struct RustMiscdev {
125-
_dev: Pin<Box<miscdev::Registration<Arc<SharedState>>>>,
128+
_dev: Pin<Box<miscdev::Registration<Pin<Arc<SharedState>>>>>,
126129
}
127130

128131
impl KernelModule for RustMiscdev {

0 commit comments

Comments
 (0)