Skip to content

Commit

Permalink
Adds a literal macro for each of the range types.
Browse files Browse the repository at this point in the history
  • Loading branch information
xfbs committed Feb 5, 2024
1 parent 4133377 commit d06fa55
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 @@ -849,6 +849,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 @@ -892,6 +911,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 @@ -418,6 +418,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 @@ -454,6 +469,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 @@ -739,6 +739,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 @@ -782,6 +801,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 @@ -352,6 +352,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 @@ -386,6 +401,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 d06fa55

Please sign in to comment.