Skip to main content

easy_cast/
impl_ops.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! `core::ops` impls.
7
8use crate::{ConvTo, Rounding};
9use core::ops::{Range, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};
10
11impl<R: Rounding, F, T: ConvTo<F, R>> ConvTo<Range<F>, R> for Range<T> {
12    type Error = T::Error;
13
14    #[inline]
15    fn try_conv_to(mode: R, n: Range<F>) -> Result<Range<T>, Self::Error> {
16        Ok(Range {
17            start: T::try_conv_to(mode, n.start)?,
18            end: T::try_conv_to(mode, n.end)?,
19        })
20    }
21
22    #[inline]
23    fn conv_to(mode: R, n: Range<F>) -> Range<T> {
24        Range {
25            start: T::conv_to(mode, n.start),
26            end: T::conv_to(mode, n.end),
27        }
28    }
29}
30
31impl<R: Rounding, F: Clone, T: ConvTo<F, R>> ConvTo<RangeInclusive<F>, R> for RangeInclusive<T> {
32    type Error = T::Error;
33
34    #[inline]
35    fn try_conv_to(mode: R, n: RangeInclusive<F>) -> Result<RangeInclusive<T>, Self::Error> {
36        let start = T::try_conv_to(mode, n.start().clone())?;
37        let end = T::try_conv_to(mode, n.end().clone())?;
38        Ok(RangeInclusive::new(start, end))
39    }
40
41    #[inline]
42    fn conv_to(mode: R, n: RangeInclusive<F>) -> RangeInclusive<T> {
43        let start = T::conv_to(mode, n.start().clone());
44        let end = T::conv_to(mode, n.end().clone());
45        RangeInclusive::new(start, end)
46    }
47}
48
49impl<R: Rounding, F, T: ConvTo<F, R>> ConvTo<RangeFrom<F>, R> for RangeFrom<T> {
50    type Error = T::Error;
51
52    #[inline]
53    fn try_conv_to(mode: R, n: RangeFrom<F>) -> Result<RangeFrom<T>, Self::Error> {
54        Ok(RangeFrom {
55            start: T::try_conv_to(mode, n.start)?,
56        })
57    }
58
59    #[inline]
60    fn conv_to(mode: R, n: RangeFrom<F>) -> RangeFrom<T> {
61        RangeFrom {
62            start: T::conv_to(mode, n.start),
63        }
64    }
65}
66
67impl<R: Rounding, F, T: ConvTo<F, R>> ConvTo<RangeTo<F>, R> for RangeTo<T> {
68    type Error = T::Error;
69
70    #[inline]
71    fn try_conv_to(mode: R, n: RangeTo<F>) -> Result<RangeTo<T>, Self::Error> {
72        Ok(RangeTo {
73            end: T::try_conv_to(mode, n.end)?,
74        })
75    }
76
77    #[inline]
78    fn conv_to(mode: R, n: RangeTo<F>) -> RangeTo<T> {
79        RangeTo {
80            end: T::conv_to(mode, n.end),
81        }
82    }
83}
84
85impl<R: Rounding, F, T: ConvTo<F, R>> ConvTo<RangeToInclusive<F>, R> for RangeToInclusive<T> {
86    type Error = T::Error;
87
88    #[inline]
89    fn try_conv_to(mode: R, n: RangeToInclusive<F>) -> Result<RangeToInclusive<T>, Self::Error> {
90        Ok(RangeToInclusive {
91            end: T::try_conv_to(mode, n.end)?,
92        })
93    }
94
95    #[inline]
96    fn conv_to(mode: R, n: RangeToInclusive<F>) -> RangeToInclusive<T> {
97        RangeToInclusive {
98            end: T::conv_to(mode, n.end),
99        }
100    }
101}