From d06fa5557fe9ebd92343ff23077b9200e38af279 Mon Sep 17 00:00:00 2001 From: Patrick Elsen Date: Wed, 31 Jan 2024 19:44:14 +0100 Subject: [PATCH] Adds a literal macro for each of the range types. --- src/inclusive_map.rs | 30 ++++++++++++++++++++++++++++++ src/inclusive_set.rs | 23 +++++++++++++++++++++++ src/map.rs | 30 ++++++++++++++++++++++++++++++ src/set.rs | 23 +++++++++++++++++++++++ 4 files changed, 106 insertions(+) diff --git a/src/inclusive_map.rs b/src/inclusive_map.rs index eedcfda..aae8dce 100644 --- a/src/inclusive_map.rs +++ b/src/inclusive_map.rs @@ -849,6 +849,25 @@ impl 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::*; @@ -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 { fn to_vec(&self) -> Vec<(RangeInclusive, V)>; } diff --git a/src/inclusive_set.rs b/src/inclusive_set.rs index 1390fc6..9461e20 100644 --- a/src/inclusive_set.rs +++ b/src/inclusive_set.rs @@ -418,6 +418,21 @@ impl From<[RangeInclusive; 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::*; @@ -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 { fn to_vec(&self) -> Vec>; } diff --git a/src/map.rs b/src/map.rs index c087857..fba5e09 100644 --- a/src/map.rs +++ b/src/map.rs @@ -739,6 +739,25 @@ impl From<[(Range, 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::*; @@ -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 { fn to_vec(&self) -> Vec<(Range, V)>; } diff --git a/src/set.rs b/src/set.rs index 47acee9..69a11bb 100644 --- a/src/set.rs +++ b/src/set.rs @@ -352,6 +352,21 @@ impl From<[Range; N]> for RangeSet { } } +/// 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::*; @@ -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 { fn to_vec(&self) -> Vec>; }