murf/action/
returns.rs

1use crate::Pointee;
2
3use super::Action;
4
5/// Creates a [`Return`] action that returns the passed `value` when called.
6pub fn return_<T>(value: T) -> Return<T> {
7    Return(value)
8}
9
10/// Action that returns the passed value `T` when called.
11#[derive(Default, Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
12pub struct Return<T>(pub T);
13
14impl<T, X> Action<X, T> for Return<T> {
15    fn exec(self, _args: X) -> T {
16        self.0
17    }
18}
19
20/// Create a [`ReturnRef`] action that returns a reference to the passed `value`
21/// when called.
22pub fn return_ref<T>(value: &T) -> ReturnRef<'_, T> {
23    ReturnRef(value)
24}
25
26/// Action that returns the passed reference `&T` when called.
27#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
28pub struct ReturnRef<'a, T>(pub &'a T);
29
30impl<'a, T, X> Action<X, &'a T> for ReturnRef<'a, T> {
31    fn exec(self, _args: X) -> &'a T {
32        self.0
33    }
34}
35
36/// Creates a [`ReturnPointee`] action that returns the value the [`Pointee`]
37/// points to when called.
38pub fn return_pointee<T>(value: T) -> ReturnPointee<T> {
39    ReturnPointee(value)
40}
41
42/// Action that returns the value the [`Pointee`] points to when called.
43#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
44pub struct ReturnPointee<T>(pub T);
45
46impl<P, T, X> Action<X, T> for ReturnPointee<P>
47where
48    P: Pointee<T>,
49{
50    fn exec(self, _args: X) -> T {
51        self.0.get()
52    }
53}