Skip to content

Commit f90442b

Browse files
committed
zephyr: sync: Conditionalize PinWeak on alloc
This only makes sense when alloc is enabled. Move the type into a module, and use it, all conditional on having alloc defined. Signed-off-by: David Brown <[email protected]>
1 parent 34e2d03 commit f90442b

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

zephyr/src/sync.rs

+30-24
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,47 @@ pub mod atomic {
2525
pub use portable_atomic::*;
2626
}
2727

28-
use core::pin::Pin;
29-
3028
#[cfg(CONFIG_RUST_ALLOC)]
3129
pub use portable_atomic_util::Arc;
3230
#[cfg(CONFIG_RUST_ALLOC)]
3331
pub use portable_atomic_util::Weak;
3432

35-
/// Safe Pinned Weak references.
36-
///
37-
/// Pin<Arc<T>> can't be converted to/from Weak safely, because there is know way to know if a given
38-
/// weak reference came from a pinned Arc. This wraps the weak reference in a new type so we know
39-
/// that it came from a pinned Arc.
40-
///
41-
/// There is a pin-weak crate that provides this for `std::sync::Arc`, but not for the one in the
42-
/// portable-atomic-utils crate.
43-
pub struct PinWeak<T>(Weak<T>);
33+
#[cfg(CONFIG_RUST_ALLOC)]
34+
mod pinweak {
35+
use core::pin::Pin;
4436

45-
impl<T> PinWeak<T> {
46-
/// Downgrade an `Pin<Arc<T>>` into a `PinWeak`.
37+
/// Safe Pinned Weak references.
4738
///
48-
/// This would be easier to use if it could be added to Arc.
49-
pub fn downgrade(this: Pin<Arc<T>>) -> Self {
50-
// SAFETY: we will never return anything other than a Pin<Arc<T>>.
51-
Self(Arc::downgrade(&unsafe { Pin::into_inner_unchecked(this) }))
52-
}
39+
/// Pin<Arc<T>> can't be converted to/from Weak safely, because there is know way to know if a given
40+
/// weak reference came from a pinned Arc. This wraps the weak reference in a new type so we know
41+
/// that it came from a pinned Arc.
42+
///
43+
/// There is a pin-weak crate that provides this for `std::sync::Arc`, but not for the one in the
44+
/// portable-atomic-utils crate.
45+
pub struct PinWeak<T>(Weak<T>);
5346

54-
/// Upgrade back to a `Pin<Arc<T>>`.
55-
pub fn upgrade(&self) -> Option<Pin<Arc<T>>> {
56-
// SAFETY: The weak was only constructed from a `Pin<Arc<T>>`.
57-
self.0
58-
.upgrade()
59-
.map(|arc| unsafe { Pin::new_unchecked(arc) })
47+
impl<T> PinWeak<T> {
48+
/// Downgrade an `Pin<Arc<T>>` into a `PinWeak`.
49+
///
50+
/// This would be easier to use if it could be added to Arc.
51+
pub fn downgrade(this: Pin<Arc<T>>) -> Self {
52+
// SAFETY: we will never return anything other than a Pin<Arc<T>>.
53+
Self(Arc::downgrade(&unsafe { Pin::into_inner_unchecked(this) }))
54+
}
55+
56+
/// Upgrade back to a `Pin<Arc<T>>`.
57+
pub fn upgrade(&self) -> Option<Pin<Arc<T>>> {
58+
// SAFETY: The weak was only constructed from a `Pin<Arc<T>>`.
59+
self.0
60+
.upgrade()
61+
.map(|arc| unsafe { Pin::new_unchecked(arc) })
62+
}
6063
}
6164
}
6265

66+
#[cfg(CONFIG_RUST_ALLOC)]
67+
pub use pinweak::*;
68+
6369
mod mutex;
6470

6571
pub use mutex::{Condvar, LockResult, Mutex, MutexGuard, TryLockError, TryLockResult};

0 commit comments

Comments
 (0)