Skip to content

Commit

Permalink
Merge pull request #77 from xfbs/derive
Browse files Browse the repository at this point in the history
Replaces manual trait implementations with derives
  • Loading branch information
jeffparsons committed Jan 31, 2024
2 parents 8b2ee44 + c9e207d commit c478b79
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 88 deletions.
12 changes: 1 addition & 11 deletions src/inclusive_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,14 @@ use serde::{
/// you can provide equivalent free functions using the `StepFnsT` type parameter.
/// [`StepLite`](crate::StepLite) is implemented for all standard integer types,
/// but not for any third party crate types.
#[derive(Clone)]
#[derive(Clone, Hash, Default)]
pub struct RangeInclusiveMap<K, V, StepFnsT = K> {
// Wrap ranges so that they are `Ord`.
// See `range_wrapper.rs` for explanation.
pub(crate) btm: BTreeMap<RangeInclusiveStartWrapper<K>, V>,
_phantom: PhantomData<StepFnsT>,
}

impl<K, V> Default for RangeInclusiveMap<K, V, K>
where
K: Ord + Clone + StepLite,
V: Eq + Clone,
{
fn default() -> Self {
Self::new()
}
}

impl<K, V, StepFnsT> PartialEq for RangeInclusiveMap<K, V, StepFnsT>
where
K: PartialEq,
Expand Down
25 changes: 8 additions & 17 deletions src/inclusive_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{
use crate::std_ext::*;
use crate::RangeInclusiveMap;

#[derive(Clone)]
#[derive(Clone, Hash, Default)]
/// A set whose items are stored as ranges bounded
/// inclusively below and above `(start..=end)`.
///
Expand All @@ -26,42 +26,33 @@ pub struct RangeInclusiveSet<T, StepFnsT = T> {
rm: RangeInclusiveMap<T, (), StepFnsT>,
}

impl<T> Default for RangeInclusiveSet<T, T>
where
T: Ord + Clone + StepLite,
{
fn default() -> Self {
Self::new()
}
}

impl<T> PartialEq for RangeInclusiveSet<T, T>
impl<T, StepFnsT> PartialEq for RangeInclusiveSet<T, StepFnsT>
where
T: PartialEq,
{
fn eq(&self, other: &RangeInclusiveSet<T, T>) -> bool {
fn eq(&self, other: &Self) -> bool {
self.rm == other.rm
}
}

impl<T> Eq for RangeInclusiveSet<T, T> where T: Eq {}
impl<T, StepFnsT> Eq for RangeInclusiveSet<T, StepFnsT> where T: Eq {}

impl<T> PartialOrd for RangeInclusiveSet<T, T>
impl<T, StepFnsT> PartialOrd for RangeInclusiveSet<T, StepFnsT>
where
T: PartialOrd,
{
#[inline]
fn partial_cmp(&self, other: &RangeInclusiveSet<T, T>) -> Option<Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.rm.partial_cmp(&other.rm)
}
}

impl<T> Ord for RangeInclusiveSet<T, T>
impl<T, StepFnsT> Ord for RangeInclusiveSet<T, StepFnsT>
where
T: Ord,
{
#[inline]
fn cmp(&self, other: &RangeInclusiveSet<T, T>) -> Ordering {
fn cmp(&self, other: &Self) -> Ordering {
self.rm.cmp(&other.rm)
}
}
Expand Down
15 changes: 1 addition & 14 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@ use serde::{
///
/// Contiguous and overlapping ranges that map to the same value
/// are coalesced into a single range.
#[derive(Clone)]
#[derive(Clone, Hash, Default, Eq)]
pub struct RangeMap<K, V> {
// Wrap ranges so that they are `Ord`.
// See `range_wrapper.rs` for explanation.
pub(crate) btm: BTreeMap<RangeStartWrapper<K>, V>,
}

impl<K, V> Default for RangeMap<K, V> {
fn default() -> Self {
Self::new()
}
}

impl<K, V> PartialEq for RangeMap<K, V>
where
K: PartialEq,
Expand Down Expand Up @@ -67,13 +61,6 @@ where
}
}

impl<K, V> Eq for RangeMap<K, V>
where
K: Eq,
V: Eq,
{
}

impl<K, V> RangeMap<K, V> {
/// Makes a new empty `RangeMap`.
#[cfg(feature = "const_fn")]
Expand Down
8 changes: 4 additions & 4 deletions src/range_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use core::ops::{Deref, Range, RangeInclusive};
// Range start wrapper
//

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash)]
pub struct RangeStartWrapper<T> {
pub end_wrapper: RangeEndWrapper<T>,
}
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<T> Deref for RangeStartWrapper<T> {
// Range end wrapper
//

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash)]
pub struct RangeEndWrapper<T> {
pub range: Range<T>,
}
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<T> Deref for RangeEndWrapper<T> {
// RangeInclusive start wrapper
//

#[derive(Eq, Debug, Clone)]
#[derive(Eq, Debug, Clone, Hash)]
pub struct RangeInclusiveStartWrapper<T> {
pub end_wrapper: RangeInclusiveEndWrapper<T>,
}
Expand Down Expand Up @@ -208,7 +208,7 @@ impl<T> Deref for RangeInclusiveStartWrapper<T> {
// RangeInclusive end wrapper
//

#[derive(Eq, Debug, Clone)]
#[derive(Eq, Debug, Clone, Hash)]
pub struct RangeInclusiveEndWrapper<T> {
pub range: RangeInclusive<T>,
}
Expand Down
43 changes: 1 addition & 42 deletions src/set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::{self, Debug};
use core::iter::FromIterator;
use core::ops::Range;
Expand All @@ -15,7 +14,7 @@ use serde::{

use crate::RangeMap;

#[derive(Clone)]
#[derive(Clone, Hash, Default, Eq, PartialEq, PartialOrd, Ord)]
/// A set whose items are stored as (half-open) ranges bounded
/// inclusively below and exclusively above `(start..end)`.
///
Expand All @@ -26,46 +25,6 @@ pub struct RangeSet<T> {
rm: RangeMap<T, ()>,
}

impl<T> Default for RangeSet<T>
where
T: Ord + Clone,
{
fn default() -> Self {
RangeSet::new()
}
}

impl<T> PartialEq for RangeSet<T>
where
T: PartialEq,
{
fn eq(&self, other: &RangeSet<T>) -> bool {
self.rm == other.rm
}
}

impl<T> Eq for RangeSet<T> where T: Eq {}

impl<T> PartialOrd for RangeSet<T>
where
T: PartialOrd,
{
#[inline]
fn partial_cmp(&self, other: &RangeSet<T>) -> Option<Ordering> {
self.rm.partial_cmp(&other.rm)
}
}

impl<T> Ord for RangeSet<T>
where
T: Ord,
{
#[inline]
fn cmp(&self, other: &RangeSet<T>) -> Ordering {
self.rm.cmp(&other.rm)
}
}

impl<T> RangeSet<T>
where
T: Ord + Clone,
Expand Down

0 comments on commit c478b79

Please sign in to comment.