Skip to content

Commit

Permalink
Merge pull request #83 from xfbs/macros
Browse files Browse the repository at this point in the history
Adds a literal macro for each of the range types.
  • Loading branch information
jeffparsons committed Feb 6, 2024
2 parents 04a911d + d06fa55 commit b2dbf07
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/inclusive_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,25 @@ impl<K: Ord + Clone + StepLite, V: Eq + Clone, const N: usize> From<[(RangeInclu
}
}

/// Create a [`RangeInclusiveMap`] from key-value pairs.
///
/// # Example
///
/// ```rust
/// # use rangemap::range_inclusive_map;
/// let map = range_inclusive_map!{
/// 0..=100 => "abc",
/// 100..=200 => "def",
/// 200..=300 => "ghi"
/// };
/// ```
#[macro_export]
macro_rules! range_inclusive_map {
($($k:expr => $v:expr),* $(,)?) => {{
<$crate::RangeInclusiveMap<_, _> as core::iter::FromIterator<_>>::from_iter([$(($k, $v),)*])
}};
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1002,6 +1021,17 @@ mod tests {
);
}

#[test]
fn test_macro() {
assert_eq!(
range_inclusive_map!(0..=100 => "abc", 100..=200 => "def", 200..=300 => "ghi"),
[(0..=100, "abc"), (100..=200, "def"), (200..=300, "ghi")]
.iter()
.cloned()
.collect(),
);
}

trait RangeInclusiveMapExt<K, V> {
fn to_vec(&self) -> Vec<(RangeInclusive<K>, V)>;
}
Expand Down
23 changes: 23 additions & 0 deletions src/inclusive_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ impl<T: Ord + Clone + StepLite, const N: usize> From<[RangeInclusive<T>; N]>
}
}

/// Create a [`RangeInclusiveSet`] from a list of ranges.
///
/// # Example
///
/// ```rust
/// # use rangemap::range_inclusive_set;
/// let set = range_inclusive_set![0..=100, 200..=300, 400..=500];
/// ```
#[macro_export]
macro_rules! range_inclusive_set {
($($range:expr),* $(,)?) => {{
<$crate::RangeInclusiveSet<_> as core::iter::FromIterator<_>>::from_iter([$($range,)*])
}};
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -539,6 +554,14 @@ mod tests {
assert_eq!(set, RangeInclusiveSet::from([0..=100, 200..=300]));
}

#[test]
fn test_macro() {
assert_eq!(
range_inclusive_set![0..=100, 200..=300, 400..=500],
[0..=100, 200..=300, 400..=500].iter().cloned().collect(),
);
}

trait RangeInclusiveSetExt<T> {
fn to_vec(&self) -> Vec<RangeInclusive<T>>;
}
Expand Down
30 changes: 30 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,25 @@ impl<K: Ord + Clone, V: Eq + Clone, const N: usize> From<[(Range<K>, V); N]> for
}
}

/// Create a [`RangeMap`] from key-value pairs.
///
/// # Example
///
/// ```rust
/// # use rangemap::range_map;
/// let map = range_map!{
/// 0..100 => "abc",
/// 100..200 => "def",
/// 200..300 => "ghi"
/// };
/// ```
#[macro_export]
macro_rules! range_map {
($($k:expr => $v:expr),* $(,)?) => {{
<$crate::RangeMap<_, _> as core::iter::FromIterator<_>>::from_iter([$(($k, $v),)*])
}};
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -889,6 +908,17 @@ mod tests {
);
}

#[test]
fn test_macro() {
assert_eq!(
range_map!(0..100 => "abc", 100..200 => "def", 200..300 => "ghi"),
[(0..100, "abc"), (100..200, "def"), (200..300, "ghi")]
.iter()
.cloned()
.collect(),
);
}

trait RangeMapExt<K, V> {
fn to_vec(&self) -> Vec<(Range<K>, V)>;
}
Expand Down
23 changes: 23 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,21 @@ impl<T: Ord + Clone, const N: usize> From<[Range<T>; N]> for RangeSet<T> {
}
}

/// Create a [`RangeSet`] from a list of ranges.
///
/// # Example
///
/// ```rust
/// # use rangemap::range_set;
/// let set = range_set![0..100, 200..300, 400..500];
/// ```
#[macro_export]
macro_rules! range_set {
($($range:expr),* $(,)?) => {{
<$crate::RangeSet<_> as core::iter::FromIterator<_>>::from_iter([$($range,)*])
}};
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -471,6 +486,14 @@ mod tests {
assert_eq!(set, RangeSet::from([0..100, 200..300]));
}

#[test]
fn test_macro() {
assert_eq!(
range_set![0..100, 200..300, 400..500],
[0..100, 200..300, 400..500].iter().cloned().collect(),
);
}

trait RangeSetExt<T> {
fn to_vec(&self) -> Vec<Range<T>>;
}
Expand Down

0 comments on commit b2dbf07

Please sign in to comment.