Pārlūkot izejas kodu

Add #[must_use]

ticki 8 gadi atpakaļ
vecāks
revīzija
528a0a48b4
2 mainītis faili ar 34 papildinājumiem un 31 dzēšanām
  1. 1 0
      src/block.rs
  2. 33 31
      src/sync.rs

+ 1 - 0
src/block.rs

@@ -25,6 +25,7 @@ use core::{ptr, cmp, mem, fmt};
 ///
 /// Accessing it through an immutable reference does not break these guarantees. That is, you are
 /// not able to read/mutate without acquiring a _mutable_ reference.
+#[must_use]
 pub struct Block {
     /// The size of this block, in bytes.
     size: usize,

+ 33 - 31
src/sync.rs

@@ -20,10 +20,43 @@ pub struct Mutex<T> {
     locked: AtomicBool,
 }
 
+impl<T> Mutex<T> {
+    /// Create a new mutex with some inner value.
+    #[inline]
+    pub const fn new(inner: T) -> Mutex<T> {
+        Mutex {
+            inner: UnsafeCell::new(inner),
+            locked: AtomicBool::new(false),
+        }
+    }
+
+    /// Lock this mutex.
+    ///
+    /// If another lock is held, this will block the thread until it is released.
+    #[inline]
+    pub fn lock(&self) -> MutexGuard<T> {
+        // Lock the mutex.
+        #[cfg(not(feature = "unsafe_no_mutex_lock"))]
+        while self.locked.compare_and_swap(false, true, atomic::Ordering::SeqCst) {
+            // ,___,
+            // {O,o}
+            // |)``)
+            // SRSLY?
+            sys::yield_now();
+        }
+
+        MutexGuard {
+            mutex: self,
+        }
+    }
+}
+
 /// A mutex guard.
 ///
 /// This acts as the lock.
+#[must_use]
 pub struct MutexGuard<'a, T: 'a> {
+    /// The parent mutex.
     mutex: &'a Mutex<T>,
 }
 
@@ -54,37 +87,6 @@ impl<'a, T> ops::DerefMut for MutexGuard<'a, T> {
     }
 }
 
-impl<T> Mutex<T> {
-    /// Create a new mutex with some inner value.
-    #[inline]
-    pub const fn new(inner: T) -> Mutex<T> {
-        Mutex {
-            inner: UnsafeCell::new(inner),
-            locked: AtomicBool::new(false),
-        }
-    }
-
-    /// Lock this mutex.
-    ///
-    /// If another lock is held, this will block the thread until it is released.
-    #[inline]
-    pub fn lock(&self) -> MutexGuard<T> {
-        // Lock the mutex.
-        #[cfg(not(feature = "unsafe_no_mutex_lock"))]
-        while self.locked.compare_and_swap(false, true, atomic::Ordering::SeqCst) {
-            // ,___,
-            // {O,o}
-            // |)``)
-            // SRSLY?
-            sys::yield_now();
-        }
-
-        MutexGuard {
-            mutex: self,
-        }
-    }
-}
-
 unsafe impl<T: Send> Send for Mutex<T> {}
 unsafe impl<T: Send> Sync for Mutex<T> {}