1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use crate::convert::{DivMeasure, IntoMeasure, MulMeasure};
use crate::unit::Unit;
use core::fmt;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::{
    Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
};

/// provide any arithmetic operation for number with measurement.
///
///
///
///
pub struct Measure<N, M> {
    pub(crate) num: N,
    _measure: PhantomData<M>,
}

///
/// impl basic func
///
///
impl<N, M> Measure<N, M> {
    ///
    /// new Measure from number.
    ///
    pub fn new(num: N) -> Self {
        Self {
            num,
            _measure: PhantomData,
        }
    }
    pub fn of(num: N) -> Self {
        Self::new(num)
    }
    pub fn num(&self) -> &N {
        &self.num
    }
    pub fn into_num(self) -> N {
        self.num
    }
}

impl<N, M> Clone for Measure<N, M>
where
    N: Clone,
{
    fn clone(&self) -> Self {
        Self::new(self.num.clone())
    }
}
impl<N, M> Copy for Measure<N, M> where N: Copy {}

impl<N, U: Unit> Debug for Measure<N, U>
where
    N: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use core::any::type_name;
        f.debug_struct("Measure")
            .field("num", &self.num)
            .field("type num", &format!("{}", type_name::<N>()))
            .field("type measure", &format!("{}", type_name::<U>()))
            .finish()
    }
}

impl<N, M> From<N> for Measure<N, M> {
    fn from(num: N) -> Self {
        Self::new(num)
    }
}

impl<N, M> Eq for Measure<N, M>
where
    Self: PartialEq<Measure<N, M>>,
    N: Eq,
{
}

impl<N, M> PartialEq for Measure<N, M>
where
    N: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.num == other.num
    }
}

impl<N, M> Neg for Measure<N, M>
where
    N: Neg,
{
    type Output = Measure<<N as Neg>::Output, M>;

    fn neg(self) -> Self::Output {
        Self::Output::new(-self.num)
    }
}

impl<NSelf, NOther, U: Unit> Add<Measure<NOther, U>> for Measure<NSelf, U>
where
    NSelf: Add<NOther>,
    U: Unit,
{
    type Output = Measure<<NSelf as Add<NOther>>::Output, U>;
    fn add(self, rhs: Measure<NOther, U>) -> Self::Output {
        Measure::new(self.num + rhs.num)
    }
}

impl<NSelf, NOther, U: Unit> AddAssign<Measure<NOther, U>> for Measure<NSelf, U>
where
    NSelf: AddAssign<NOther>,
    U: Unit,
{
    fn add_assign(&mut self, rhs: Measure<NOther, U>) {
        self.num += rhs.num
    }
}

impl<NSelf, NOther, U: Unit> Sub<Measure<NOther, U>> for Measure<NSelf, U>
where
    NSelf: Sub<NOther>,
    U: Unit,
{
    type Output = Measure<<NSelf as Sub<NOther>>::Output, U>;
    fn sub(self, rhs: Measure<NOther, U>) -> Self::Output {
        Measure::new(self.num - rhs.num)
    }
}

impl<NSelf, NOther, U: Unit> SubAssign<Measure<NOther, U>> for Measure<NSelf, U>
where
    NSelf: SubAssign<NOther>,
    U: Unit,
{
    fn sub_assign(&mut self, rhs: Measure<NOther, U>) {
        self.num -= rhs.num
    }
}

impl<NSelf, USelf, Rhs> Mul<Rhs> for Measure<NSelf, USelf>
where
    Rhs: IntoMeasure,
    NSelf: Mul<<Rhs as IntoMeasure>::Num>,
    USelf: Unit + MulMeasure<<Rhs as IntoMeasure>::Unit, NSelf, <Rhs as IntoMeasure>::Num>,
{
    type Output = Measure<
        <NSelf as Mul<<Rhs as IntoMeasure>::Num>>::Output,
        <USelf as MulMeasure<<Rhs as IntoMeasure>::Unit, NSelf, <Rhs as IntoMeasure>::Num>>::Output,
    >;

    fn mul(self, rhs: Rhs) -> Self::Output {
        let rhs = rhs.into_measure();
        let num = self.num * rhs.num;
        Self::Output::new(num)
    }
}

impl<NSelf, M, NRhs> MulAssign<NRhs> for Measure<NSelf, M>
where
    NSelf: MulAssign<NRhs>,
{
    fn mul_assign(&mut self, rhs: NRhs) {
        self.num *= rhs;
    }
}

impl<NSelf, USelf, Rhs> Div<Rhs> for Measure<NSelf, USelf>
where
    Rhs: IntoMeasure,
    NSelf: Div<<Rhs as IntoMeasure>::Num>,
    USelf: Unit + DivMeasure<<Rhs as IntoMeasure>::Unit, NSelf, <Rhs as IntoMeasure>::Num>,
{
    type Output = Measure<
        <NSelf as Div<<Rhs as IntoMeasure>::Num>>::Output,
        <USelf as DivMeasure<<Rhs as IntoMeasure>::Unit, NSelf, <Rhs as IntoMeasure>::Num>>::Output,
    >;

    fn div(self, rhs: Rhs) -> Self::Output {
        let rhs = rhs.into_measure();
        let num = self.num / rhs.num;
        Self::Output::new(num)
    }
}

impl<NSelf, M, NRhs> DivAssign<NRhs> for Measure<NSelf, M>
where
    NSelf: DivAssign<NRhs>,
{
    fn div_assign(&mut self, rhs: NRhs) {
        self.num /= rhs;
    }
}

impl<NSelf, M, NRhs> Rem<NRhs> for Measure<NSelf, M>
where
    NSelf: Rem<NRhs>,
{
    type Output = Measure<<NSelf as Rem<NRhs>>::Output, M>;

    fn rem(self, rhs: NRhs) -> Self::Output {
        Self::Output::new(self.num % rhs)
    }
}

impl<NSelf, M, NRhs> RemAssign<NRhs> for Measure<NSelf, M>
where
    NSelf: RemAssign<NRhs>,
{
    fn rem_assign(&mut self, rhs: NRhs) {
        self.num %= rhs
    }
}

impl<N, M> PartialOrd for Measure<N, M>
where
    Self: PartialEq<Measure<N, M>>,
    N: PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.num.partial_cmp(&other.num)
    }
}
impl<N, M> Ord for Measure<N, M>
where
    Self: Eq + PartialOrd<Measure<N, M>>,
    N: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.num.cmp(&other.num)
    }
}

#[cfg(test)]
mod tests {
    use crate::convert::{DivMeasure, MulMeasure};
    use crate::unit::Unit;
    use crate::Measure;
    use std::cmp::Ordering;
    use std::fmt::Formatter;
    use std::ops::{Div, Mul};

    struct USome;
    impl Unit for USome {}
    type SomeUnit<N> = Measure<N, USome>;

    struct USomeMul;
    impl Unit for USomeMul {}
    type SomeUnitMul<N> = Measure<N, USomeMul>;
    impl<N: Mul<N>> MulMeasure<USome, N> for USome {
        type Output = USomeMul;
    }

    struct USomeDiv;
    impl Unit for USomeDiv {}
    type SomeUnitDiv<N> = Measure<N, USomeDiv>;
    impl<N: Div<N>> DivMeasure<USome, N> for USome {
        type Output = USomeDiv;
    }

    #[test]
    fn debug() {
        let some = SomeUnit::new(1);
        println!("{:?}", &some)
    }
    #[test]
    fn format_default() {
        impl crate::fmt::Symbol<()> for USome {
            fn fmt(f: &mut Formatter<'_>) -> core::fmt::Result {
                f.write_str("unit")
            }
        }
        let some = SomeUnit::new(1);
        assert_eq!(format!("{}", &some), "1 unit");
    }
    #[test]
    fn neg() {
        assert_eq!(SomeUnit::new(-1), -SomeUnit::new(1))
    }
    #[test]
    fn add() {
        let one = SomeUnit::new(1);
        let two = SomeUnit::new(2);
        let three = one + two;
        assert_eq!(3, three.into_num());
    }
    #[test]
    fn add_assign() {
        let mut lhs = SomeUnit::new(1);
        let rhs = SomeUnit::new(2);
        lhs += rhs;
        assert_eq!(3, lhs.into_num());
    }
    #[test]
    fn sub() {
        let three = SomeUnit::new(3);
        let two = SomeUnit::new(2);
        let one = three - two;
        assert_eq!(1, one.into_num());
    }
    #[test]
    fn sub_assign() {
        let mut lhs = SomeUnit::of(1);
        let rhs = SomeUnit::new(2);
        lhs += rhs;
        assert_eq!(3, *lhs.num());
    }
    #[test]
    fn mul_num() {
        let two = SomeUnit::new(2);
        let three = 3;
        let six = two * three;
        assert_eq!(6, six.into_num());
    }
    #[test]
    fn mul_num_assign() {
        let mut lhs = SomeUnit::of(2);
        let rhs = 3;
        lhs *= rhs;
        assert_eq!(6, lhs.into_num());
    }
    #[test]
    fn mul_unit() {
        let lhs = SomeUnit::of(2);
        let rhs = SomeUnit::of(3);
        let mul = lhs * rhs;
        assert_eq!(SomeUnitMul::of(6), mul)
    }
    #[test]
    fn div_num() {
        let six = SomeUnit::new(6);
        let three = 3;
        let two = six / three;
        assert_eq!(2, two.into_num());
    }
    #[test]
    fn div_num_assign() {
        let mut lhs = SomeUnit::of(6);
        let rhs = 3;
        lhs /= rhs;
        assert_eq!(2, lhs.into_num())
    }
    #[test]
    fn div_unit() {
        let lhs = SomeUnit::of(6);
        let rhs = SomeUnit::of(3);
        let div = lhs / rhs;
        assert_eq!(SomeUnitDiv::of(2), div)
    }
    #[test]
    fn partial_ord_eq() {
        let lhs = SomeUnit::of(1.0);
        let rhs = SomeUnit::of(1.0);
        assert_eq!(Some(Ordering::Equal), lhs.partial_cmp(&rhs));
        assert_eq!(false, lhs < rhs);
        assert_eq!(true, lhs <= rhs);
        assert_eq!(true, lhs >= rhs);
        assert_eq!(false, lhs > rhs);
    }
    #[test]
    fn partial_ord_gt() {
        let lhs = SomeUnit::of(2.0);
        let rhs = SomeUnit::of(1.0);
        assert_eq!(Some(Ordering::Greater), lhs.partial_cmp(&rhs));
        assert_eq!(false, lhs < rhs);
        assert_eq!(false, lhs <= rhs);
        assert_eq!(true, lhs >= rhs);
        assert_eq!(true, lhs > rhs);
    }
    #[test]
    fn partial_ord_lt() {
        let lhs = SomeUnit::of(1.0);
        let rhs = SomeUnit::of(2.0);
        assert_eq!(Some(Ordering::Less), lhs.partial_cmp(&rhs));
        assert_eq!(true, lhs < rhs);
        assert_eq!(true, lhs <= rhs);
        assert_eq!(false, lhs >= rhs);
        assert_eq!(false, lhs > rhs);
    }
    #[test]
    fn ord_eq() {
        let lhs = SomeUnit::of(1);
        let rhs = SomeUnit::of(1);
        assert_eq!(Ordering::Equal, lhs.cmp(&rhs));
        assert_eq!(false, lhs < rhs);
        assert_eq!(true, lhs <= rhs);
        assert_eq!(true, lhs >= rhs);
        assert_eq!(false, lhs > rhs);
    }
    #[test]
    fn ord_gt() {
        let lhs = SomeUnit::of(2);
        let rhs = SomeUnit::of(1);
        assert_eq!(Ordering::Greater, lhs.cmp(&rhs));
        assert_eq!(false, lhs < rhs);
        assert_eq!(false, lhs <= rhs);
        assert_eq!(true, lhs >= rhs);
        assert_eq!(true, lhs > rhs);
    }
    #[test]
    fn ord_lt() {
        let lhs = SomeUnit::of(1);
        let rhs = SomeUnit::of(2);
        assert_eq!(Ordering::Less, lhs.cmp(&rhs));
        assert_eq!(true, lhs < rhs);
        assert_eq!(true, lhs <= rhs);
        assert_eq!(false, lhs >= rhs);
        assert_eq!(false, lhs > rhs);
    }
    #[test]
    fn from() {
        let one = SomeUnit::from(1);
        assert_eq!(1, one.into_num());
    }
    #[test]
    fn into() {
        let one: SomeUnit<_> = 1.into();
        assert_eq!(1, one.into_num());
    }
}