upget/
lib.rs

1/*! Provider of [`Upget`].
2
3*The author of this crate is not good at English.*
4*Forgive me if the document is hard to read.*
5
6The [`Upget`] trate can be used for refactoring as follows.
7
8Before refactoring.
9```
10fn clone_with_sort(vec: &Vec<String>) -> Vec<String> {
11    let mut result = vec.clone();
12    result.sort();
13    result
14}
15```
16
17After refactoring.
18```
19# use crate::upget::Upget;
20fn clone_with_sort(vec: &Vec<String>) -> Vec<String> {
21    vec.clone().upget(|x| x.sort())
22}
23```
24*/
25
26#![no_std]
27#![warn(missing_docs)]
28
29pub mod prelude;
30
31/// Trait that handles common patterns of updating and getting value.
32pub trait Upget {
33    /// Update self in the specified way and get the result.
34    ///
35    /// # Examples
36    ///
37    /// ```
38    /// # use crate::upget::Upget;
39    /// assert_eq!([3, 2, 1].upget(|x| x.sort()), [1, 2, 3]);
40    /// ```
41    fn upget<F>(mut self, f: F) -> Self
42    where
43        Self: Sized,
44        F: FnOnce(&mut Self),
45    {
46        f(&mut self);
47        self
48    }
49}
50
51impl<T> Upget for T {}