From 1ba939b6400c7c6cec0a3e90f5f3a2808647078b Mon Sep 17 00:00:00 2001 From: david-perez Date: Thu, 7 Sep 2023 13:41:46 +0200 Subject: [PATCH 1/2] Make `Filters` constructible from `IntoIterator`s This just relaxes bounds. Useful to create filters from arrays. --- src/filters.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/filters.rs b/src/filters.rs index 8ec59916..b9d63367 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -9,8 +9,11 @@ pub struct Filters { rules: Vec<(Regex, String)>, } -impl<'a> From> for Filters { - fn from(value: Vec<(&'a str, &'a str)>) -> Filters { +impl<'a, T> From for Filters +where + T: IntoIterator, +{ + fn from(value: T) -> Filters { let mut rv = Filters::default(); for (regex, replacement) in value { rv.add(regex, replacement); @@ -58,3 +61,9 @@ fn test_filters() { "hellohello [NAME] [a]bc" ); } + +#[test] +fn test_static_str_array_conversion() { + let arr: [(&'static str, &'static str); 2] = [("a1", "b1"), ("a2", "b2")]; + let _ = Filters::from(arr); +} From 6220413d17f628920ddb8afd0de2ccebbf8e7d36 Mon Sep 17 00:00:00 2001 From: david-perez Date: Sun, 8 Oct 2023 13:10:46 +0200 Subject: [PATCH 2/2] Make `Filters` implement `FromIterator<(&str, &str)>` --- src/filters.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/filters.rs b/src/filters.rs index b9d63367..be0485ee 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::iter::FromIterator; use regex::Regex; @@ -9,13 +10,16 @@ pub struct Filters { rules: Vec<(Regex, String)>, } -impl<'a, T> From for Filters -where - T: IntoIterator, -{ - fn from(value: T) -> Filters { +impl<'a> From> for Filters { + fn from(value: Vec<(&'a str, &'a str)>) -> Self { + Self::from_iter(value) + } +} + +impl<'a> FromIterator<(&'a str, &'a str)> for Filters { + fn from_iter>(iter: I) -> Self { let mut rv = Filters::default(); - for (regex, replacement) in value { + for (regex, replacement) in iter { rv.add(regex, replacement); } rv @@ -65,5 +69,11 @@ fn test_filters() { #[test] fn test_static_str_array_conversion() { let arr: [(&'static str, &'static str); 2] = [("a1", "b1"), ("a2", "b2")]; - let _ = Filters::from(arr); + let _ = Filters::from_iter(arr); +} + +#[test] +fn test_vec_str_conversion() { + let vec: Vec<(&str, &str)> = Vec::from([("a1", "b1"), ("a2", "b2")]); + let _ = Filters::from(vec); }