hypercpu/
value.rs

1use crate::Moment;
2use crate::ops::*;
3use async_trait::async_trait;
4use std::ops;
5
6
7/// A `Moment`-exclusive operable value.
8/// 
9/// Performing operations on this type will return
10/// `Moment`s that perform the operation on the
11/// value instead of the standard compiler
12/// behaviors.
13#[derive(Clone)]
14pub struct Value<T>(T);
15
16impl<T> Value<T> {
17  /// Create a new `Value` from a value.
18  pub fn new(value: T) -> Self {
19    Self(value)
20  }
21
22  /// Get the inner value.
23  pub fn into_inner(self) -> T {
24    self.0
25  }
26}
27
28#[async_trait]
29impl<T> Moment for Value<T>
30where
31  T: Moment
32{
33  type Value = Self;
34
35  async fn resolve(self) -> Self::Value {
36    self
37  }
38}
39
40impl<L, R, O> ops::Add<R> for Value<L>
41where
42  L: Moment,
43  R: Moment,
44  O: Moment,
45  L::Value: ops::Add<R::Value, Output = O>
46{
47  type Output = Add<L, R, O>;
48
49  fn add(self, rhs: R) -> Self::Output {
50    Add::new(self.into_inner(), rhs)
51  }
52}
53
54impl<L, R, O> ops::Sub<R> for Value<L>
55where
56  L: Moment,
57  R: Moment,
58  O: Moment,
59  L::Value: ops::Sub<R::Value, Output = O>
60{
61  type Output = Sub<L, R, O>;
62
63  fn sub(self, rhs: R) -> Self::Output {
64    Sub::new(self.into_inner(), rhs)
65  }
66}
67
68impl<L, R, O> ops::Mul<R> for Value<L>
69where
70  L: Moment,
71  R: Moment,
72  O: Moment,
73  L::Value: ops::Mul<R::Value, Output = O>
74{
75  type Output = Mul<L, R, O>;
76
77  fn mul(self, rhs: R) -> Self::Output {
78    Mul::new(self.into_inner(), rhs)
79  }
80}
81
82impl<L, R, O> ops::Div<R> for Value<L>
83where
84  L: Moment,
85  R: Moment,
86  O: Moment,
87  L::Value: ops::Div<R::Value, Output = O>
88{
89  type Output = Div<L, R, O>;
90
91  fn div(self, rhs: R) -> Self::Output {
92    Div::new(self.into_inner(), rhs)
93  }
94}
95
96impl<L, R, O> ops::Rem<R> for Value<L>
97where
98  L: Moment,
99  R: Moment,
100  O: Moment,
101  L::Value: ops::Rem<R::Value, Output = O>
102{
103  type Output = Rem<L, R, O>;
104
105  fn rem(self, rhs: R) -> Self::Output {
106    Rem::new(self.into_inner(), rhs)
107  }
108}