Skip to content

Commit

Permalink
Merge pull request #95 from ModProg/manual-default
Browse files Browse the repository at this point in the history
Implement Default manually
  • Loading branch information
jeffparsons authored Feb 24, 2024
2 parents 21056cf + 5fde6a9 commit 9c4adef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/inclusive_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,23 @@ use serde::{
/// you can provide equivalent free functions using the `StepFnsT` type parameter.
/// [`StepLite`] is implemented for all standard integer types,
/// but not for any third party crate types.
#[derive(Clone, Default)]
#[derive(Clone)]
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, StepFnsT> Default for RangeInclusiveMap<K, V, StepFnsT> {
fn default() -> Self {
Self {
btm: BTreeMap::default(),
_phantom: PhantomData,
}
}
}

impl<K, V, StepFnsT> Hash for RangeInclusiveMap<K, V, StepFnsT>
where
K: Hash,
Expand Down Expand Up @@ -1869,6 +1878,14 @@ mod tests {
assert_eq!(format!("{:?}", map), "{2..=5: (), 7..=8: (), 10..=11: ()}");
}

// impl Default where T: ?Default

#[test]
fn always_default() {
struct NoDefault;
RangeInclusiveMap::<NoDefault, NoDefault>::default();
}

// impl Serialize

#[cfg(feature = "serde1")]
Expand Down
18 changes: 17 additions & 1 deletion src/inclusive_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub type Intersection<'a, T> = crate::operations::Intersection<'a, RangeInclusiv
/// Union iterator over two [`RangeInclusiveSet`].
pub type Union<'a, T> = crate::operations::Union<'a, RangeInclusive<T>, Iter<'a, T>>;

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

impl<T, StepFnsT> Default for RangeInclusiveSet<T, StepFnsT> {
fn default() -> Self {
Self {
rm: RangeInclusiveMap::default(),
}
}
}

impl<T> RangeInclusiveSet<T, T>
where
T: Ord + Clone + StepLite,
Expand Down Expand Up @@ -777,6 +785,14 @@ mod tests {
assert_eq!(format!("{:?}", set), "{2..=5, 7..=8, 10..=11}");
}

// impl Default where T: ?Default

#[test]
fn always_default() {
struct NoDefault;
RangeInclusiveSet::<NoDefault>::default();
}

// impl Serialize

#[cfg(feature = "serde1")]
Expand Down
18 changes: 17 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ use serde::{
///
/// Contiguous and overlapping ranges that map to the same value
/// are coalesced into a single range.
#[derive(Clone, Default, Eq)]
#[derive(Clone, 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 {
btm: BTreeMap::default(),
}
}
}

impl<K, V> Hash for RangeMap<K, V>
where
K: Hash,
Expand Down Expand Up @@ -1702,6 +1710,14 @@ mod tests {
assert_eq!(format!("{:?}", map), "{2..5: (), 6..7: (), 8..9: ()}");
}

// impl Default where T: ?Default

#[test]
fn always_default() {
struct NoDefault;
RangeMap::<NoDefault, NoDefault>::default();
}

// Iterator Tests

#[test]
Expand Down
18 changes: 17 additions & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub type Intersection<'a, T> = crate::operations::Intersection<'a, Range<T>, Ite
/// Union iterator over two [`RangeSet`].
pub type Union<'a, T> = crate::operations::Union<'a, Range<T>, Iter<'a, T>>;

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

impl<T> Default for RangeSet<T> {
fn default() -> Self {
Self {
rm: RangeMap::default(),
}
}
}

impl<T> RangeSet<T>
where
T: Ord + Clone,
Expand Down Expand Up @@ -746,6 +754,14 @@ mod tests {
assert_eq!(format!("{:?}", set), "{2..5, 7..8, 10..11}");
}

// impl Default where T: ?Default

#[test]
fn always_default() {
struct NoDefault;
RangeSet::<NoDefault>::default();
}

// impl Serialize

#[cfg(feature = "serde1")]
Expand Down

0 comments on commit 9c4adef

Please sign in to comment.