run_down/guard.rs
1// Copyright 2019 Brian Gianforcaro
2
3use crate::rundown_ref::RundownRef;
4
5/// An RAII implementation of a "scoped lock" pattern, but specialized
6/// to the needs of run-down protection. When this structure is dropped
7/// (falls out of scope), the rundown protection reference that was
8/// previously acquired is released.
9///
10/// This structure is created by the `try_acquire` method on `RundownRef`.
11///
12/// This type attempts to follow the RAII guidance here:
13/// <https://github.com/rust-unofficial/patterns/blob/master/patterns/RAII.md>
14pub struct RundownGuard<'r> {
15 /// The run-dwon reference that this guard objec points too.
16 owned_run_down_ref: &'r RundownRef,
17}
18
19impl<'r> RundownGuard<'r> {
20 /// Creates a new [`RundownGuard`] which owns an instance of run-down
21 /// protection on the [`RundownRef`] provided.
22 ///
23 /// # Arguments
24 ///
25 /// * `owned_run_down_ref` - The run-down reference to release when the
26 /// guard goes out of scope.
27 ///
28 pub const fn new(owned_run_down_ref: &'r RundownRef) -> RundownGuard<'r> {
29 Self { owned_run_down_ref }
30 }
31}
32
33impl<'r> Drop for RundownGuard<'r> {
34 /// Releases the previously acquired instance of run-down protection.
35 fn drop(&mut self) {
36 self.owned_run_down_ref.release()
37 }
38}