Skip to main content

easy_cast/
impl_range.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::range` impls.
7
8use crate::{ConvTo, Rounding};
9use core::range::{Range, RangeFrom, RangeInclusive, 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 last = T::try_conv_to(mode, n.last.clone())?;
38        Ok(RangeInclusive { start, last })
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 last = T::conv_to(mode, n.last.clone());
45        RangeInclusive { start, last }
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<RangeToInclusive<F>, R> for RangeToInclusive<T> {
68    type Error = T::Error;
69
70    #[inline]
71    fn try_conv_to(mode: R, n: RangeToInclusive<F>) -> Result<RangeToInclusive<T>, Self::Error> {
72        Ok(RangeToInclusive {
73            last: T::try_conv_to(mode, n.last)?,
74        })
75    }
76
77    #[inline]
78    fn conv_to(mode: R, n: RangeToInclusive<F>) -> RangeToInclusive<T> {
79        RangeToInclusive {
80            last: T::conv_to(mode, n.last),
81        }
82    }
83}