From a296ee085c55d67396122e3679fe0faa136b5068 Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Sat, 15 Mar 2025 12:15:03 -0700 Subject: [PATCH] Stop returning a value from ScopeGuarding::commit This was unused outside of the tests. --- src/common.rs | 22 +++++++++------------- src/tests/common.rs | 23 ++--------------------- 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/src/common.rs b/src/common.rs index d4a8a32da..a9a964df2 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1711,11 +1711,9 @@ pub fn new(value: T, on_drop: F) -> Self { Self(Some((value, on_drop))) } - /// Invokes the callback and returns the wrapped value, consuming the ScopeGuard. - pub fn commit(mut guard: Self) -> T { - let (mut value, on_drop) = guard.0.take().expect("Should always have Some value"); - on_drop(&mut value); - value + /// Invokes the callback, consuming the ScopeGuard. + pub fn commit(guard: Self) { + std::mem::drop(guard) } /// Cancels the invocation of the callback, returning the original wrapped value. @@ -1750,17 +1748,15 @@ fn drop(&mut self) { /// A trait expressing what ScopeGuard can do. This is necessary because scoped_push returns an /// `impl Trait` object and therefore methods on ScopeGuard which take a self parameter cannot be /// used. -pub trait ScopeGuarding: DerefMut { - /// Invokes the callback and returns the wrapped value, consuming the ScopeGuard. - fn commit(guard: Self) -> Self::Target; -} - -impl ScopeGuarding for ScopeGuard { - fn commit(guard: Self) -> T { - ScopeGuard::commit(guard) +pub trait ScopeGuarding: DerefMut + Sized { + /// Invokes the callback, consuming the guard. + fn commit(guard: Self) { + std::mem::drop(guard); } } +impl ScopeGuarding for ScopeGuard {} + /// A scoped manager to save the current value of some variable, and set it to a new value. When /// dropped, it restores the variable to its old value. pub fn scoped_push( diff --git a/src/tests/common.rs b/src/tests/common.rs index e1330d3ea..ed8a35835 100644 --- a/src/tests/common.rs +++ b/src/tests/common.rs @@ -1,4 +1,4 @@ -use crate::common::{scoped_push, truncate_at_nul, ScopeGuard, ScopeGuarding}; +use crate::common::{scoped_push, truncate_at_nul, ScopeGuard}; use crate::wchar::prelude::*; #[test] @@ -44,30 +44,11 @@ fn test_scope_guard() { counter.fetch_add(1, relaxed); }); assert_eq!(counter.load(relaxed), 1); - let val = ScopeGuard::commit(guard); + ScopeGuard::commit(guard); assert_eq!(counter.load(relaxed), 2); - assert_eq!(val, 123); } } -#[test] -fn test_scope_guard_consume() { - // The following pattern works. - struct Storage { - value: &'static str, - } - let obj = Storage { value: "nu" }; - assert_eq!(obj.value, "nu"); - let obj = scoped_push(obj, |obj| &mut obj.value, "mu"); - assert_eq!(obj.value, "mu"); - let obj = scoped_push(obj, |obj| &mut obj.value, "mu2"); - assert_eq!(obj.value, "mu2"); - let obj = ScopeGuarding::commit(obj); - assert_eq!(obj.value, "mu"); - let obj = ScopeGuarding::commit(obj); - assert_eq!(obj.value, "nu"); -} - #[test] fn test_truncate_at_nul() { assert_eq!(truncate_at_nul(L!("abc\0def")), L!("abc"));