Move `no-std` sync implementations to a folder to clean up
authorMatt Corallo <git@bluematt.me>
Tue, 10 Jan 2023 06:26:46 +0000 (06:26 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 10 Jan 2023 06:26:46 +0000 (06:26 +0000)
lightning/src/lib.rs
lightning/src/sync.rs [deleted file]
lightning/src/sync/mod.rs [new file with mode: 0644]
lightning/src/sync/nostd_sync.rs [new file with mode: 0644]

index 1f3ab47b1ae3f7d2ca8802e46c267d5428e3822c..1ec4d6aa5a5eab8a5fcd0bd14f36110876bd0898 100644 (file)
@@ -180,15 +180,4 @@ mod debug_sync;
 #[cfg(all(not(feature = "_bench_unstable"), feature = "backtrace", feature = "std", test))]
 extern crate backtrace;
 
-#[cfg(feature = "std")]
-mod sync {
-       #[cfg(all(not(feature = "_bench_unstable"), test))]
-       pub use crate::debug_sync::*;
-       #[cfg(any(feature = "_bench_unstable", not(test)))]
-       pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
-       #[cfg(any(feature = "_bench_unstable", not(test)))]
-       pub use crate::util::fairrwlock::FairRwLock;
-}
-
-#[cfg(not(feature = "std"))]
 mod sync;
diff --git a/lightning/src/sync.rs b/lightning/src/sync.rs
deleted file mode 100644 (file)
index caf88a7..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-pub use ::alloc::sync::Arc;
-use core::ops::{Deref, DerefMut};
-use core::time::Duration;
-use core::cell::{RefCell, Ref, RefMut};
-
-pub type LockResult<Guard> = Result<Guard, ()>;
-
-pub struct Condvar {}
-
-impl Condvar {
-       pub fn new() -> Condvar {
-               Condvar { }
-       }
-
-       pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
-               Ok(guard)
-       }
-
-       #[allow(unused)]
-       pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
-               Ok((guard, ()))
-       }
-
-       pub fn notify_all(&self) {}
-}
-
-pub struct Mutex<T: ?Sized> {
-       inner: RefCell<T>
-}
-
-#[must_use = "if unused the Mutex will immediately unlock"]
-pub struct MutexGuard<'a, T: ?Sized + 'a> {
-       lock: RefMut<'a, T>,
-}
-
-impl<T: ?Sized> Deref for MutexGuard<'_, T> {
-       type Target = T;
-
-       fn deref(&self) -> &T {
-               &self.lock.deref()
-       }
-}
-
-impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
-       fn deref_mut(&mut self) -> &mut T {
-               self.lock.deref_mut()
-       }
-}
-
-impl<T> Mutex<T> {
-       pub fn new(inner: T) -> Mutex<T> {
-               Mutex { inner: RefCell::new(inner) }
-       }
-
-       pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
-               Ok(MutexGuard { lock: self.inner.borrow_mut() })
-       }
-
-       pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
-               Ok(MutexGuard { lock: self.inner.borrow_mut() })
-       }
-}
-
-pub struct RwLock<T: ?Sized> {
-       inner: RefCell<T>
-}
-
-pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
-       lock: Ref<'a, T>,
-}
-
-pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
-       lock: RefMut<'a, T>,
-}
-
-impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
-       type Target = T;
-
-       fn deref(&self) -> &T {
-               &self.lock.deref()
-       }
-}
-
-impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
-       type Target = T;
-
-       fn deref(&self) -> &T {
-               &self.lock.deref()
-       }
-}
-
-impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
-       fn deref_mut(&mut self) -> &mut T {
-               self.lock.deref_mut()
-       }
-}
-
-impl<T> RwLock<T> {
-       pub fn new(inner: T) -> RwLock<T> {
-               RwLock { inner: RefCell::new(inner) }
-       }
-
-       pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
-               Ok(RwLockReadGuard { lock: self.inner.borrow() })
-       }
-
-       pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
-               Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
-       }
-
-       pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
-               match self.inner.try_borrow_mut() {
-                       Ok(lock) => Ok(RwLockWriteGuard { lock }),
-                       Err(_) => Err(())
-               }
-       }
-}
-
-pub type FairRwLock<T> = RwLock<T>;
diff --git a/lightning/src/sync/mod.rs b/lightning/src/sync/mod.rs
new file mode 100644 (file)
index 0000000..5843380
--- /dev/null
@@ -0,0 +1,11 @@
+#[cfg(all(feature = "std", not(feature = "_bench_unstable"), test))]
+pub use crate::debug_sync::*;
+#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
+pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
+#[cfg(all(feature = "std", any(feature = "_bench_unstable", not(test))))]
+pub use crate::util::fairrwlock::FairRwLock;
+
+#[cfg(not(feature = "std"))]
+mod nostd_sync;
+#[cfg(not(feature = "std"))]
+pub use nostd_sync::*;
diff --git a/lightning/src/sync/nostd_sync.rs b/lightning/src/sync/nostd_sync.rs
new file mode 100644 (file)
index 0000000..caf88a7
--- /dev/null
@@ -0,0 +1,119 @@
+pub use ::alloc::sync::Arc;
+use core::ops::{Deref, DerefMut};
+use core::time::Duration;
+use core::cell::{RefCell, Ref, RefMut};
+
+pub type LockResult<Guard> = Result<Guard, ()>;
+
+pub struct Condvar {}
+
+impl Condvar {
+       pub fn new() -> Condvar {
+               Condvar { }
+       }
+
+       pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
+               Ok(guard)
+       }
+
+       #[allow(unused)]
+       pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
+               Ok((guard, ()))
+       }
+
+       pub fn notify_all(&self) {}
+}
+
+pub struct Mutex<T: ?Sized> {
+       inner: RefCell<T>
+}
+
+#[must_use = "if unused the Mutex will immediately unlock"]
+pub struct MutexGuard<'a, T: ?Sized + 'a> {
+       lock: RefMut<'a, T>,
+}
+
+impl<T: ?Sized> Deref for MutexGuard<'_, T> {
+       type Target = T;
+
+       fn deref(&self) -> &T {
+               &self.lock.deref()
+       }
+}
+
+impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
+       fn deref_mut(&mut self) -> &mut T {
+               self.lock.deref_mut()
+       }
+}
+
+impl<T> Mutex<T> {
+       pub fn new(inner: T) -> Mutex<T> {
+               Mutex { inner: RefCell::new(inner) }
+       }
+
+       pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
+               Ok(MutexGuard { lock: self.inner.borrow_mut() })
+       }
+
+       pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
+               Ok(MutexGuard { lock: self.inner.borrow_mut() })
+       }
+}
+
+pub struct RwLock<T: ?Sized> {
+       inner: RefCell<T>
+}
+
+pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
+       lock: Ref<'a, T>,
+}
+
+pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
+       lock: RefMut<'a, T>,
+}
+
+impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
+       type Target = T;
+
+       fn deref(&self) -> &T {
+               &self.lock.deref()
+       }
+}
+
+impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
+       type Target = T;
+
+       fn deref(&self) -> &T {
+               &self.lock.deref()
+       }
+}
+
+impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
+       fn deref_mut(&mut self) -> &mut T {
+               self.lock.deref_mut()
+       }
+}
+
+impl<T> RwLock<T> {
+       pub fn new(inner: T) -> RwLock<T> {
+               RwLock { inner: RefCell::new(inner) }
+       }
+
+       pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
+               Ok(RwLockReadGuard { lock: self.inner.borrow() })
+       }
+
+       pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
+               Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
+       }
+
+       pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
+               match self.inner.try_borrow_mut() {
+                       Ok(lock) => Ok(RwLockWriteGuard { lock }),
+                       Err(_) => Err(())
+               }
+       }
+}
+
+pub type FairRwLock<T> = RwLock<T>;