From 576578baf4025686ae4c2893c2aafbc8d1e14722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=90=AF=E8=88=AA?= Date: Fri, 24 Nov 2023 15:32:18 +0800 Subject: [PATCH] Improve aquisition logic --- framework/jinux-frame/src/sync/mutex.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/framework/jinux-frame/src/sync/mutex.rs b/framework/jinux-frame/src/sync/mutex.rs index ab043bb57..1e64144d5 100644 --- a/framework/jinux-frame/src/sync/mutex.rs +++ b/framework/jinux-frame/src/sync/mutex.rs @@ -33,11 +33,7 @@ impl Mutex { pub fn try_lock(&self) -> Option> { // Cannot be reduced to `then_some`, or the possible dropping of the temporary // guard will cause an unexpected unlock. - // - // TODO: Remove the lint suppression when the false positive warning is fixed - // upstream. - #[allow(clippy::unnecessary_lazy_evaluations)] - self.acquire_lock().then(|| MutexGuard { mutex: self }) + self.acquire_lock().then(|| MutexGuard::new(self)) } /// Release the mutex and wake up one thread which is blocked on this mutex. @@ -66,10 +62,17 @@ impl fmt::Debug for Mutex { unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} +#[clippy::has_significant_drop] pub struct MutexGuard<'a, T> { mutex: &'a Mutex, } +impl<'a, T> MutexGuard<'a, T> { + fn new(mutex: &'a Mutex) -> MutexGuard<'a, T> { + MutexGuard { mutex } + } +} + impl<'a, T> Deref for MutexGuard<'a, T> { type Target = T;