Skip to content

Commit

Permalink
refactor: remove type bound from struct def (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinui0 authored Feb 1, 2024
1 parent c9a8c0b commit 368980f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions utils/src/range/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ use std::ops::{Add, Range, Sub};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(from = "Vec<Range<T>>", into = "Vec<Range<T>>")
serde(
bound = "for<'a> T: serde::Serialize + serde::de::Deserialize<'a> + Copy + Ord",
from = "Vec<Range<T>>",
into = "Vec<Range<T>>"
)
)]
pub struct RangeSet<T: Copy + Ord> {
pub struct RangeSet<T> {
/// The ranges of the set.
///
/// The ranges *MUST* be sorted, non-adjacent, non-intersecting, and non-empty.
Expand All @@ -62,7 +66,7 @@ impl<T: Copy + Ord> From<Vec<Range<T>>> for RangeSet<T> {
}
}

impl<T: Copy + Ord> From<RangeSet<T>> for Vec<Range<T>> {
impl<T> From<RangeSet<T>> for Vec<Range<T>> {
fn from(ranges: RangeSet<T>) -> Self {
ranges.into_inner()
}
Expand All @@ -76,6 +80,18 @@ impl<T: Copy + Ord> Default for RangeSet<T> {
}
}

impl<T> RangeSet<T> {
/// Returns the ranges of the set.
pub fn into_inner(self) -> Vec<Range<T>> {
self.ranges
}

/// Returns the number of ranges in the set.
pub fn len_ranges(&self) -> usize {
self.ranges.len()
}
}

impl<T: Copy + Ord> RangeSet<T> {
/// Returns a new `RangeSet` from the given ranges.
///
Expand All @@ -93,11 +109,6 @@ impl<T: Copy + Ord> RangeSet<T> {
set
}

/// Returns the ranges of the set.
pub fn into_inner(self) -> Vec<Range<T>> {
self.ranges
}

/// Returns an iterator over the values in the set.
pub fn iter(&self) -> RangeSetIter<'_, T> {
RangeSetIter {
Expand All @@ -113,11 +124,6 @@ impl<T: Copy + Ord> RangeSet<T> {
}
}

/// Returns the number of ranges in the set.
pub fn len_ranges(&self) -> usize {
self.ranges.len()
}

/// Returns `true` if the set contains the given value.
pub fn contains(&self, value: &T) -> bool {
self.ranges.iter().any(|range| range.contains(value))
Expand Down

0 comments on commit 368980f

Please sign in to comment.