Skip to content

Commit

Permalink
Merge pull request #84 from xfbs/from
Browse files Browse the repository at this point in the history
Implements `From<[_; T]>` for set and maps
  • Loading branch information
jeffparsons authored Feb 2, 2024
2 parents c50ed21 + 5ff63c9 commit 4133377
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 @@ -837,6 +837,18 @@ 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::*;
Expand Down Expand Up @@ -869,6 +881,17 @@ mod tests {
}
}

#[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 @@ -406,6 +406,18 @@ 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::*;
Expand Down Expand Up @@ -434,6 +446,14 @@ mod tests {
}
}

#[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 @@ -729,6 +729,16 @@ 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::*;
Expand Down Expand Up @@ -761,6 +771,17 @@ mod tests {
}
}

#[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 @@ -342,6 +342,16 @@ 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::*;
Expand All @@ -368,6 +378,14 @@ mod tests {
}
}

#[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 4133377

Please sign in to comment.