Stop returning a value from ScopeGuarding::commit

This was unused outside of the tests.
This commit is contained in:
Peter Ammon
2025-03-15 12:15:03 -07:00
parent 0113db3ff7
commit a296ee085c
2 changed files with 11 additions and 34 deletions

View File

@@ -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<T, F: FnOnce(&mut T)> ScopeGuarding for ScopeGuard<T, F> {
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<T, F: FnOnce(&mut T)> ScopeGuarding for ScopeGuard<T, F> {}
/// 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<Context, Accessor, T>(

View File

@@ -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"));