Skip to content

Commit

Permalink
Implements From array for set and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
xfbs committed Jan 31, 2024
1 parent 8b2ee44 commit 5ff63c9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/inclusive_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,11 +847,34 @@ where
}
}

impl<K: Ord + Clone + StepLite, V: Eq + Clone, const N: usize> From<[(RangeInclusive<K>, V); N]>
for RangeInclusiveMap<K, V>
{
fn from(value: [(RangeInclusive<K>, 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<K, V> {
fn to_vec(&self) -> Vec<(RangeInclusive<K>, V)>;
}
Expand Down
20 changes: 20 additions & 0 deletions src/inclusive_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,31 @@ where
}
}

impl<T: Ord + Clone + StepLite, const N: usize> From<[RangeInclusive<T>; N]>
for RangeInclusiveSet<T>
{
fn from(value: [RangeInclusive<T>; 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<T> {
fn to_vec(&self) -> Vec<RangeInclusive<T>>;
}
Expand Down
21 changes: 21 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,32 @@ where
}
}

impl<K: Ord + Clone, V: Eq + Clone, const N: usize> From<[(Range<K>, V); N]> for RangeMap<K, V> {
fn from(value: [(Range<K>, 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<K, V> {
fn to_vec(&self) -> Vec<(Range<K>, V)>;
}
Expand Down
18 changes: 18 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,29 @@ where
}
}

impl<T: Ord + Clone, const N: usize> From<[Range<T>; N]> for RangeSet<T> {
fn from(value: [Range<T>; 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<T> {
fn to_vec(&self) -> Vec<Range<T>>;
}
Expand Down

0 comments on commit 5ff63c9

Please sign in to comment.