Lines Matching full:guard
4 //! A scope guard will run a given closure when it goes out of scope,
12 //! This example creates a scope guard with an example function:
18 //! let _guard = scopeguard::guard((), |_| {
24 //! // Here, at the end of `_guard`'s scope, the guard's closure is called.
43 //! // use a cell to observe drops during and after the scope guard is active
46 //! // Create a scope guard using `defer!` for the current scope
62 //! ## Scope Guard with Value
64 //! If the scope guard closure needs to access an outer value that is also
65 //! mutated outside of the scope guard, then you may want to use the scope guard
66 //! with a value. The guard works like a smart pointer, so the inner value can
69 //! ### 1. The guard owns a file
71 //! In this example, the scope guard owns a file and ensures pending writes are
90 //! let mut file = scopeguard::guard(f, |f| {
94 //! // Access the file through the scope guard itself
104 //! ### 2. The guard restores an invariant on scope exit
122 //! // We *must* use a scope guard to run this code safely. We
124 //! // The scope guard ensures we restore the invariant after successful
140 //! // Use a scope guard with a value.
143 //! // The scope guard owns the hole, but we can access it through the guard.
144 //! let mut hole_guard = scopeguard::guard(hole, |hole| {
202 /// Return `true` if the guard’s associated code should run
259 let _guard = $crate::guard((), |()| { $($t)* });
291 /// `ScopeGuard` is a scope guard that may own a protected value.
293 /// If you place a guard in a local variable, the closure can
301 /// The guard's closure will be called with the held value in the destructor.
323 /// The `Strategy` decides whether the scope guard's closure should run.
334 /// “Defuse” the guard and extract the value without calling the closure.
339 /// use scopeguard::{guard, ScopeGuard};
344 /// let mut guard = guard(Vec::new(), |mut v| v.clear());
345 /// guard.push(1);
349 /// // “defuse” the guard and get back its inner parts
350 /// let value = ScopeGuard::into_inner(guard);
352 /// // guard still exists in this branch
357 pub fn into_inner(guard: Self) -> T { in into_inner()
359 // so `ptr::read` the value and forget the guard. in into_inner()
360 let mut guard = ManuallyDrop::new(guard); in into_inner() localVariable
362 let value = ptr::read(&*guard.value); in into_inner()
366 ManuallyDrop::drop(&mut guard.dropfn); in into_inner()
375 pub fn guard<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, Always> in guard() function
401 /// For performance reasons, or to emulate “only run guard on unwind” in
402 /// no-std environments, we can also use the default guard and simply manually
413 /// let guard = scopeguard::guard((), |_| {});
418 /// ScopeGuard::into_inner(guard);
434 // The guard does not store any instance of S, so it is also irrelevant.
556 let value = guard((), |()| value_drops.set(1 + value_drops.get())); in test_only_dropped_by_closure_when_run()
558 let guard = guard(value, |_| closure_drops.set(1 + closure_drops.get())); in test_only_dropped_by_closure_when_run() localVariable
561 drop(guard); in test_only_dropped_by_closure_when_run()
570 let value = guard((), |()| value_drops.set(1 + value_drops.get())); in test_dropped_once_when_not_run()
572 let captured = guard((), |()| captured_drops.set(1 + captured_drops.get())); in test_dropped_once_when_not_run()
574 let guard = guard_on_unwind(value, |value| { in test_dropped_once_when_not_run() localVariable
582 drop(guard); in test_dropped_once_when_not_run()
591 let value = guard(42, |_| dropped.set(true)); in test_into_inner()
592 let guard = guard(value, |_| dropped.set(true)); in test_into_inner() localVariable
593 let inner = ScopeGuard::into_inner(guard); in test_into_inner()