orx_closure/
fun.rs

1/// Function trait representing `In -> Out` transformation.
2///
3/// It provides the common interface for closures, such as `Closure<Capture, In, Out>`, over all capture types.
4///
5/// Furthermore, this trait enables to forget about the capture, or equivalently drop the `Capture` generic parameter, by using `dyn Fun<In, Out>` trait object.
6///
7/// # Relation with `Fn`
8///
9/// `Fun<In, Out>` can be considered equivalent to `Fn(In) -> Out`.
10/// The reason it co-exists is that it is not possible to implement `fn_traits` in stable version.
11///
12/// However, all that implements `Fn(In) -> Out` also auto-implements `Fun<In, Out>`.
13pub trait Fun<In, Out> {
14    /// Calls the function with the given `input` and returns the produced output.
15    fn call(&self, input: In) -> Out;
16}
17impl<In, Out, F: Fn(In) -> Out> Fun<In, Out> for F {
18    fn call(&self, input: In) -> Out {
19        self(input)
20    }
21}
22
23/// Function trait representing `In -> &Out` transformation.
24///
25/// It provides the common interface for closures, such as `ClosureRef<Capture, In, Out>`, over all capture types.
26///
27/// Furthermore, this trait enables to forget about the capture, or equivalently drop the `Capture` generic parameter, by using `dyn FunRef<In, Out>` trait object.
28///
29/// # Relation with `Fn`
30///
31/// `FunRef<In, Out>` can be considered equivalent to `Fn(In) -> &Out`.
32///
33/// However, it appears to be impossible to have an instance of the latter due to lifetime errors.
34/// Therefore, `FunRef<In, Out>` is required.
35pub trait FunRef<In, Out: ?Sized> {
36    /// Calls the function with the given `input` and returns the produced output.
37    fn call(&self, input: In) -> &Out;
38}
39
40/// Function trait representing `In -> Option<&Out>` transformation.
41///
42/// It provides the common interface for closures, such as `ClosureOptRef<Capture, In, Out>`, over all capture types.
43///
44/// Furthermore, this trait enables to forget about the capture, or equivalently drop the `Capture` generic parameter, by using `dyn FunOptRef<In, Out>` trait object.
45///
46/// # Relation with `Fn`
47///
48/// `FunOptRef<In, Out>` can be considered equivalent to `Fn(In) -> Option<&Out>`.
49///
50/// However, it appears to be impossible to have an instance of the latter due to lifetime errors.
51/// Therefore, `FunOptRef<In, Out>` is required.
52pub trait FunOptRef<In, Out: ?Sized> {
53    /// Calls the function with the given `input` and returns the produced output.
54    fn call(&self, input: In) -> Option<&Out>;
55}
56
57/// Function trait representing `In -> Result<&Out, Error>` transformation.
58///
59/// It provides the common interface for closures, such as `ClosureResRef<Capture, In, Out>`, over all capture types.
60///
61/// Furthermore, this trait enables to forget about the capture, or equivalently drop the `Capture` generic parameter, by using `dyn FunOptRef<In, Out>` trait object.
62///
63/// # Relation with `Fn`
64///
65/// `FunResRef<In, Out, Error>` can be considered equivalent to `Fn(In) -> Result<&Out, Error>`.
66///
67/// However, it appears to be impossible to have an instance of the latter due to lifetime errors.
68/// Therefore, `FunResRef<In, Out, Error>` is required.
69pub trait FunResRef<In, Out: ?Sized, Error> {
70    /// Calls the function with the given `input` and returns the produced output.
71    fn call(&self, input: In) -> Result<&Out, Error>;
72}