Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
grishasobol committed Dec 19, 2023
1 parent c019a1f commit 0a2cb94
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 85 deletions.
28 changes: 8 additions & 20 deletions common/numerated/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ impl<T: Numerated> Interval<T> {
let (start, end) = (self.start, self.end);
debug_assert!(start <= end, "It's guaranteed by `Interval`");
start.inc_if_lt(end).map(|start| {
debug_assert_impl!(
start <= self.end,
"for each `s: T`, `e: T`, s < e ⇒ s.inc_if_lt(e) ≤ e"
);
debug_assert!(start <= end, "`T: Numerated` impl error");
Interval { start, end }
})
}
Expand All @@ -97,10 +94,7 @@ impl<T: Numerated> Interval<T> {
let (start, end) = (range.start, range.end);
end.dec_if_gt(start)
.map(|end| {
debug_assert_impl!(
start <= end,
"for each `s: T`, `e: T`, s > e ⇒ s.dec_if_gt(e) ≥ e"
);
debug_assert!(start <= end, "`T: Numerated` impl error");
Self { start, end }
})
.ok_or(if start == end {
Expand Down Expand Up @@ -133,26 +127,23 @@ impl<T: Numerated> From<Interval<T>> for RangeInclusive<T> {
impl<T: Numerated> From<T> for Interval<T> {
fn from(point: T) -> Self {
let (start, end) = (point, point);
debug_assert!(start <= end, "Must be guaranteed by `T: Ord`");
debug_assert!(start <= end, "`T: Ord` impl error");
Self { start, end }
}
}

impl<T: Numerated + LowerBounded> From<RangeToInclusive<T>> for Interval<T> {
fn from(range: RangeToInclusive<T>) -> Self {
let (start, end) = (T::min_value(), range.end);
debug_assert!(start <= end, "Must be guaranteed by `T: LowerBounded`");
debug_assert!(start <= end, "`T: LowerBounded` impl error");
Self { start, end }
}
}

impl<T: Numerated + UpperBounded + LowerBounded> From<RangeFull> for Interval<T> {
fn from(_: RangeFull) -> Self {
let (start, end) = (T::min_value(), T::max_value());
debug_assert!(
start <= end,
"Must be guaranteed by `T: UpperBounded + LowerBounded`"
);
debug_assert!(start <= end, "`T: UpperBounded + LowerBounded` impl error");
Self { start, end }
}
}
Expand Down Expand Up @@ -181,7 +172,7 @@ impl<T: Numerated + UpperBounded, I: Into<T::Bound>> TryFrom<RangeFrom<I>> for I
match Into::<T::Bound>::into(range.start).unbound() {
Some(start) => {
let end = T::max_value();
debug_assert!(start <= end, "Must be guaranteed by `T: UpperBounded`");
debug_assert!(start <= end, "`T: UpperBounded` impl error");
Ok(Self { start, end })
}
None => Err(EmptyRangeError),
Expand All @@ -204,7 +195,7 @@ impl<T: Numerated + LowerBounded + UpperBounded, I: Into<T::Bound>> TryFrom<Rang
let start = T::min_value();
end.dec_if_gt(start)
.map(|end| {
debug_assert!(start <= end, "Must be guaranteed by `T: LowerBounded`");
debug_assert!(start <= end, "`T: LowerBounded` impl error");
Self { start, end }
})
.ok_or(EmptyRangeError)
Expand Down Expand Up @@ -286,10 +277,7 @@ impl<T: Numerated + UpperBounded> Interval<T> {
start
.add_if_enclosed_by(distance, T::max_value())
.map(|end| {
debug_assert_impl!(
start <= end,
"for each `s: T`, `e: T`, `d: T::Distance`, s ≤ e ⇒ s.add_if_enclosed_by(d, e) ≤ e"
);
debug_assert!(start <= end, "`T: Numerated` impl error");
Self { start, end }
})
.ok_or(OutOfBoundsError.into())
Expand Down
45 changes: 9 additions & 36 deletions common/numerated/src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
//! [IntervalIterator], [VoidsIterator], [DifferenceIterator] implementations.

use crate::{
interval::{IncorrectOrEmptyRangeError, NewWithLenError, OutOfBoundsError},
IncorrectRangeError, Interval, Numerated,
interval::{
IncorrectOrEmptyRangeError, IncorrectRangeError, NewWithLenError, OutOfBoundsError,
},
Interval, Numerated,
};
use core::{
fmt::{self, Debug, Display, Formatter},
Expand All @@ -47,26 +49,6 @@ impl<T: Numerated> IntervalIterator<T> {
}
}

/// Error which occurs when trying to convert empty [IntervalIterator] into [Interval] or [RangeInclusive].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TryFromIntervalError;

impl<T: Numerated> TryFrom<IntervalIterator<T>> for Interval<T> {
type Error = TryFromIntervalError;

fn try_from(interval: IntervalIterator<T>) -> Result<Self, Self::Error> {
interval.0.ok_or(TryFromIntervalError)
}
}

impl<T: Numerated> TryFrom<IntervalIterator<T>> for RangeInclusive<T> {
type Error = TryFromIntervalError;

fn try_from(interval: IntervalIterator<T>) -> Result<Self, Self::Error> {
Interval::try_from(interval).map(Into::into)
}
}

impl<T: Numerated> From<Interval<T>> for IntervalIterator<T> {
fn from(interval: Interval<T>) -> Self {
Self(Some(interval))
Expand Down Expand Up @@ -226,10 +208,7 @@ impl<T: Numerated, I: Iterator<Item = Interval<T>>> Iterator for DifferenceItera
} else {
if let Some(new_start) = interval2.end().inc_if_lt(interval1.end()) {
self.interval1 = Interval::new(new_start, interval1.end());
debug_assert_impl!(
self.interval1.is_some(),
"for each x: T, y: T, x < y ⇒ x.inc_if_lt(y) ≤ y"
);
debug_assert!(self.interval1.is_some(), "`T: Numerated` impl error");
} else if interval1.end() == interval2.end() {
self.interval1 = None;
self.interval2 = None;
Expand All @@ -239,10 +218,7 @@ impl<T: Numerated, I: Iterator<Item = Interval<T>>> Iterator for DifferenceItera

if let Some(new_end) = interval2.start().dec_if_gt(interval1.start()) {
let res = Interval::new(interval1.start(), new_end);
debug_assert_impl!(
res.is_some(),
"for each x: T, y: T, x > y ⇒ x.dec_if_gt(y) ≥ y"
);
debug_assert!(res.is_some(), "`T: Numerated` impl error");
return res;
} else {
continue;
Expand Down Expand Up @@ -271,19 +247,16 @@ impl<T: Numerated, I: Iterator<Item = Interval<T>>> Iterator for VoidsIterator<T
debug_assert!(interval.start() < start);

let Some(void_end) = start.dec_if_gt(interval.start()) else {
debug_assert_impl!("for each x: T, y: T, x > y ⇒ x.dec_if_gt(y) == Some(_)");
debug_assert!(false, "`T: Numerated` impl error");
return None;
};

let res = Interval::new(interval.start(), void_end);
debug_assert_impl!(
res.is_some(),
"for each x: T, y: T, x > y ⇒ x.dec_if_gt(y) ≥ y"
);
debug_assert!(res.is_some(), "`T: Numerated` impl error");

if let Some(new_start) = end.inc_if_lt(interval.end()) {
let Some(new_interval) = Interval::new(new_start, interval.end()) else {
debug_assert_impl!("for each x: T, y: T, x < y ⇒ x.inc_if_lt(y) ≤ y");
debug_assert!(false, "`T: Numerated` impl error");
return None;
};
*interval = new_interval;
Expand Down
22 changes: 4 additions & 18 deletions common/numerated/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,18 @@

extern crate alloc;

macro_rules! debug_assert_impl {
($info:expr) => {
debug_assert_impl!(false, $info);
};
($condition:expr, $info:expr) => {
debug_assert!($condition, "`T: Numerated` impl error: {}", $info);
};
}

mod interval;
pub mod interval;
mod iterators;
mod numerated;
mod tree;

pub use crate::{
interval::{EmptyRangeError, IncorrectRangeError, Interval},
iterators::{DifferenceIterator, IntervalIterator, TryFromIntervalError, VoidsIterator},
interval::Interval,
iterators::{DifferenceIterator, IntervalIterator, VoidsIterator},
numerated::{Bound, Numerated, OptionBound},
tree::IntervalsTree,
};

pub use num_traits::{
self,
bounds::{LowerBounded, UpperBounded},
CheckedAdd, One, Zero,
};
pub use num_traits;

#[cfg(any(feature = "mock", test))]
pub mod mock;
Expand Down
7 changes: 3 additions & 4 deletions common/numerated/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

//! Mock for crate property testing and also can be used in other crates for their numerated types impls.

use crate::{Bound, Interval, IntervalIterator, IntervalsTree, Numerated, One, Zero};
use crate::{Bound, IntervalIterator, IntervalsTree, Numerated};
use alloc::{collections::BTreeSet, fmt::Debug, vec::Vec};
use num_traits::bounds::UpperBounded;
use num_traits::{bounds::UpperBounded, One, Zero};

/// Mock function for any [Numerated] implementation testing.
pub fn test_numerated<T>(x: T, y: T)
Expand Down Expand Up @@ -87,10 +87,9 @@ where
if start.unbound() == end.unbound() {
assert!(i.is_empty());
assert_eq!(i.inner(), None);
assert_eq!(Interval::try_from(i).ok(), None);
} else {
assert!(!i.is_empty());
let i = Interval::try_from(i).unwrap();
let i = i.inner().unwrap();
assert_eq!(i.start(), start.unbound().unwrap());
match end.unbound() {
Some(e) => assert_eq!(i.end(), e.dec_if_gt(i.start()).unwrap()),
Expand Down
16 changes: 9 additions & 7 deletions common/numerated/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl<T: Numerated> IntervalsTree<T> {

if let Some(right_end) = right_end.inc_if_lt(start) {
if right_end == start {
debug_assert!(right_start <= end, "Must be cause of method it was found");
self.inner.insert(right_start, end);
} else {
self.inner.insert(start, end);
Expand Down Expand Up @@ -233,14 +234,15 @@ impl<T: Numerated> IntervalsTree<T> {
let end = right_end.max(end);

let Some((left_start, left_end)) = left_interval else {
debug_assert!(start <= end, "Must be cause of method it was found");
self.inner.insert(start, end);
return;
};

debug_assert!(left_end < right_start);
debug_assert!(left_start <= start);
let Some(left_end) = left_end.inc_if_lt(right_start) else {
debug_assert_impl!("for each x: T, y: T, x < y ⇒ x.inc_if_lt(y) == Some(_)");
debug_assert!(false, "`T: Numerated` impl error");
return;
};

Expand Down Expand Up @@ -302,27 +304,27 @@ impl<T: Numerated> IntervalsTree<T> {
}

if let Some(start) = start.dec_if_gt(right_start) {
debug_assert!(start >= right_start);
debug_assert!(right_start <= start, "`T: Numerated` impl error");
self.inner.insert(right_start, start);
} else {
debug_assert!(right_start <= end);
debug_assert!(right_start <= end, "Must be cause of method it was found");
self.inner.remove(&right_start);
}

if let Some(end) = end.inc_if_lt(right_end) {
debug_assert!(end <= right_end);
debug_assert!(end <= right_end, "`T: Numerated` impl error");
self.inner.insert(end, right_end);
} else {
debug_assert!(start <= right_end);
}

if let Some(left_start) = left_interval {
// `left_start` < `start` cause of method it was found.
debug_assert!(left_start < start);
debug_assert!(left_start < start, "Must be cause of method it was found");
if let Some(start) = start.dec_if_gt(left_start) {
debug_assert!(left_start <= start, "`T: Numerated` impl error");
self.inner.insert(left_start, start);
} else {
debug_assert_impl!("for each x: T, y: T, x > y ⇒ x.dec_if_gt(y) == Some(_)");
debug_assert!(false, "`T: Numerated` impl error");
}
}
}
Expand Down

0 comments on commit 0a2cb94

Please sign in to comment.