mavio/utils/
update.rs

1/// <sup>`⚠`</sup>
2/// A trait, that updates internal state of a type using an instance of another type.
3pub trait TryUpdateFrom<T> {
4    /// Error, that may be thrown during update.
5    type Error;
6
7    /// <sup>`⚠`</sup>
8    /// Performs the update.
9    ///
10    /// **⚠** If you are going to reimplement the blanket implementation, then it is mandatory, that
11    /// all checks related to the possibility of update should be performed before the update.
12    ///
13    /// Returns [`Self::Error`], when update is impossible.
14    fn try_update_from(&mut self, value: T) -> Result<(), Self::Error> {
15        self.check_try_update_from(&value)?;
16        unsafe {
17            self.update_from_unchecked(value);
18        }
19        Ok(())
20    }
21
22    /// <sup>`⚠`</sup>
23    /// Checks, that update is possible.
24    ///
25    /// Returns [`Self::Error`], when update is impossible.
26    fn check_try_update_from(&self, value: &T) -> Result<(), Self::Error>;
27
28    /// <sup>`⚠`</sup>
29    /// Performs the update without checking, whether update is possible.
30    ///
31    /// **⚠** This method should not be used directly. Use [`Self::try_update_from`] instead.
32    ///
33    /// # Panics
34    ///
35    /// This method should panic, when update is not possible. It is marked as `unsafe` since
36    /// Rust type system can't guarantee, that the contract will be fulfilled by implementor.
37    unsafe fn update_from_unchecked(&mut self, value: T);
38}