Skip to content

Commit f5cc3eb

Browse files
committed
add FromIterator trait
1 parent cabde6d commit f5cc3eb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/url_search_params.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,29 @@ where
246246
}
247247
}
248248

249+
#[cfg(feature = "std")]
250+
impl<Input> FromIterator<(Input, Input)> for URLSearchParams
251+
where
252+
Input: AsRef<str>,
253+
{
254+
/// Converts an iterator to URLSearchParams
255+
///
256+
/// ```
257+
/// use ada_url::URLSearchParams;
258+
/// let iterator = std::iter::repeat(("hello", "world")).take(5);
259+
/// let params = URLSearchParams::from_iter(iterator);
260+
/// assert_eq!(params.len(), 5);
261+
/// ```
262+
fn from_iter<T: IntoIterator<Item = (Input, Input)>>(iter: T) -> Self {
263+
let mut params = URLSearchParams::parse("")
264+
.expect("Failed to parse empty string. This is likely due to a bug");
265+
for item in iter {
266+
params.append(item.0.as_ref(), item.1.as_ref());
267+
}
268+
params
269+
}
270+
}
271+
249272
pub struct URLSearchParamsKeysIterator<'a> {
250273
iterator: *mut ffi::ada_url_search_params_keys_iter,
251274
_phantom: core::marker::PhantomData<&'a str>,

0 commit comments

Comments
 (0)