x_bow/
guarantee.rs

1use std::cell::{Ref, RefMut};
2
3use crate::{borrow_mut_guard::BorrowMutGuard, path_ext::PathExt, Path};
4
5/// Implemented for [Path] objects that will always be able to obtain their
6/// data piece.
7///
8/// Such paths are those that
9/// *   don't go through any `enum` (if the enum is in a different variant,
10///     then the data cannot be obtained)
11/// *   don't go through `Vec` or `HashMap` (the requested item might not
12///     be in the collection)
13///
14/// The methods in this trait are similar to the borrow methods in [PathExt],
15/// but they don't return [Option] because we know the data is always there.
16pub trait PathExtGuaranteed: PathExt {
17    /// Borrow the data at this path immutably.
18    ///
19    /// See [borrow_opt][PathExt::borrow_opt] for more details.
20    fn borrow(&self) -> Ref<'_, <Self as Path>::Out> {
21        self.borrow_opt().unwrap()
22    }
23    /// Borrow the data at this path mutably, notifying all the relevant
24    /// change listeners when the returned borrow guard is dropped.
25    ///
26    /// See [borrow_opt_mut][PathExt::borrow_opt_mut] for more details.
27    fn borrow_mut(&self) -> BorrowMutGuard<'_, Self> {
28        self.borrow_opt_mut().unwrap()
29    }
30
31    /// Borrow the data at this path mutably **without notifying** any listener.
32    ///
33    /// See [borrow_opt_mut_without_notifying][PathExt::borrow_opt_mut_without_notifying]
34    /// for more details.
35    fn borrow_mut_without_notifying(&self) -> RefMut<'_, <Self as Path>::Out> {
36        self.borrow_opt_mut_without_notifying().unwrap()
37    }
38
39    /// Clone the data identified by this path.
40    ///
41    /// Equivalent to `path.borrow().clone()`.
42    fn get(&self) -> Self::Out
43    where
44        Self::Out: Clone,
45    {
46        self.borrow().clone()
47    }
48
49    /// Set the data identified by this path, notifying listeners.
50    ///
51    /// Equivalent to `*path.borrow_mut() = data;`
52    fn set(&self, data: Self::Out)
53    where
54        Self::Out: Sized,
55    {
56        *self.borrow_mut() = data;
57    }
58}