From 5ff63c9815a95f1b56992fc7674ddbe03134b18b Mon Sep 17 00:00:00 2001 From: Patrick Elsen Date: Wed, 31 Jan 2024 20:02:40 +0100 Subject: [PATCH] Implements From array for set and maps --- src/inclusive_map.rs | 23 +++++++++++++++++++++++ src/inclusive_set.rs | 20 ++++++++++++++++++++ src/map.rs | 21 +++++++++++++++++++++ src/set.rs | 18 ++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/src/inclusive_map.rs b/src/inclusive_map.rs index 15351be..fa4001a 100644 --- a/src/inclusive_map.rs +++ b/src/inclusive_map.rs @@ -847,11 +847,34 @@ where } } +impl From<[(RangeInclusive, V); N]> + for RangeInclusiveMap +{ + fn from(value: [(RangeInclusive, V); N]) -> Self { + let mut map = Self::new(); + for (range, value) in IntoIterator::into_iter(value) { + map.insert(range, value); + } + map + } +} + #[cfg(test)] mod tests { use super::*; use alloc::{format, vec, vec::Vec}; + #[test] + fn test_from_array() { + let mut map = RangeInclusiveMap::new(); + map.insert(0..=100, "hello"); + map.insert(200..=300, "world"); + assert_eq!( + map, + RangeInclusiveMap::from([(0..=100, "hello"), (200..=300, "world")]) + ); + } + trait RangeInclusiveMapExt { fn to_vec(&self) -> Vec<(RangeInclusive, V)>; } diff --git a/src/inclusive_set.rs b/src/inclusive_set.rs index ff99a49..9824d34 100644 --- a/src/inclusive_set.rs +++ b/src/inclusive_set.rs @@ -415,11 +415,31 @@ where } } +impl From<[RangeInclusive; N]> + for RangeInclusiveSet +{ + fn from(value: [RangeInclusive; N]) -> Self { + let mut set = Self::new(); + for value in IntoIterator::into_iter(value) { + set.insert(value); + } + set + } +} + #[cfg(test)] mod tests { use super::*; use alloc::{format, vec, vec::Vec}; + #[test] + fn test_from_array() { + let mut set = RangeInclusiveSet::new(); + set.insert(0..=100); + set.insert(200..=300); + assert_eq!(set, RangeInclusiveSet::from([0..=100, 200..=300])); + } + trait RangeInclusiveSetExt { fn to_vec(&self) -> Vec>; } diff --git a/src/map.rs b/src/map.rs index 761b247..009a6ce 100644 --- a/src/map.rs +++ b/src/map.rs @@ -742,11 +742,32 @@ where } } +impl From<[(Range, V); N]> for RangeMap { + fn from(value: [(Range, V); N]) -> Self { + let mut map = Self::new(); + for (range, value) in IntoIterator::into_iter(value) { + map.insert(range, value); + } + map + } +} + #[cfg(test)] mod tests { use super::*; use alloc::{format, vec, vec::Vec}; + #[test] + fn test_from_array() { + let mut map = RangeMap::new(); + map.insert(0..100, "hello"); + map.insert(200..300, "world"); + assert_eq!( + map, + RangeMap::from([(0..100, "hello"), (200..300, "world")]) + ); + } + trait RangeMapExt { fn to_vec(&self) -> Vec<(Range, V)>; } diff --git a/src/set.rs b/src/set.rs index e375244..ef1bb07 100644 --- a/src/set.rs +++ b/src/set.rs @@ -383,11 +383,29 @@ where } } +impl From<[Range; N]> for RangeSet { + fn from(value: [Range; N]) -> Self { + let mut set = Self::new(); + for value in IntoIterator::into_iter(value) { + set.insert(value); + } + set + } +} + #[cfg(test)] mod tests { use super::*; use alloc::{format, vec, vec::Vec}; + #[test] + fn test_from_array() { + let mut set = RangeSet::new(); + set.insert(0..100); + set.insert(200..300); + assert_eq!(set, RangeSet::from([0..100, 200..300])); + } + trait RangeSetExt { fn to_vec(&self) -> Vec>; }