Forráskód Böngészése

Add form_urlencoded::Parse::into_owned

Simon Sapin 10 éve
szülő
commit
224aee62d7
1 módosított fájl, 12 hozzáadás és 0 törlés
  1. 12 0
      src/form_urlencoded.rs

+ 12 - 0
src/form_urlencoded.rs

@@ -16,6 +16,7 @@
 use encoding::EncodingOverride;
 use percent_encoding::{percent_encode_byte, percent_decode};
 use std::borrow::{Borrow, Cow};
+use std::iter;
 use std::str;
 
 
@@ -79,6 +80,7 @@ pub fn parse_with_encoding<'a>(input: &'a [u8],
 }
 
 /// The return type of `parse()`.
+#[derive(Copy, Clone)]
 pub struct Parse<'a> {
     input: &'a [u8],
     encoding: EncodingOverride,
@@ -134,6 +136,16 @@ fn replace_plus<'a>(input: &'a [u8]) -> Cow<'a, [u8]> {
     }
 }
 
+impl<'a> Parse<'a> {
+    /// Return a new iterator that yields pairs of `String` instead of pair of `Cow<str>`.
+    pub fn into_owned(self) -> iter::Map<Parse<'a>, fn((Cow<str>, Cow<str>)) -> (String, String)> {
+        fn into_owned((k, v): (Cow<str>, Cow<str>)) -> (String, String) {
+            (k.into_owned(), v.into_owned())
+        }
+        self.map(into_owned)
+    }
+}
+
 /// The [`application/x-www-form-urlencoded` byte serializer](
 /// https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer).
 ///