From 61986d1a612212fe12c37d376e5f9cf8ee77f3a4 Mon Sep 17 00:00:00 2001 From: kana-rus Date: Sat, 20 Apr 2024 19:43:05 +0900 Subject: [PATCH] Fix: `Clone`, `from_iter` --- ohkami/src/response/headers.rs | 40 +++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/ohkami/src/response/headers.rs b/ohkami/src/response/headers.rs index 0b9f6311..f3160bb4 100644 --- a/ohkami/src/response/headers.rs +++ b/ohkami/src/response/headers.rs @@ -3,6 +3,7 @@ use crate::__internal__::Append; use rustc_hash::FxHashMap; +#[derive(Clone)] pub struct Headers { standard: Box<[Option>; N_SERVER_HEADERS]>, custom: Option>>>, @@ -531,22 +532,17 @@ const _: () = { } } - impl Clone for Headers { - fn clone(&self) -> Self { - Self::from_iter( - self.iter() - .map(|(k, v)| ( - unsafe {Header::from_bytes(k.as_bytes()).unwrap_unchecked()}, - String::from(v) - ))) - } - } - impl Headers { - pub fn from_iter(iter: impl IntoIterator>)>) -> Self { + pub fn from_iter(iter: impl IntoIterator>)> + ) -> Self { let mut this = Headers::new(); for (k, v) in iter { - this.insert(k, v.into()) + match Header::from_bytes(k.as_bytes()) { + Some(h) => this.insert(h, v.into()), + None => {this.set().custom(k, v.into());} + } } this } @@ -566,3 +562,21 @@ const _: () = { } } }; + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +#[cfg(feature="rt_worker")] +#[cfg(test)] +#[test] fn test_response_headers_into_worker() { + assert_eq!( + Into::<::worker::Headers>::into(Headers::from_iter([ + ("Content-Type", "application/json"), + ("Content-Length", "42"), + ("Vary", "Origin"), + ])).0, + ::worker::Headers::from_iter([ + ("Content-Type", "application/json"), + ("Content-Length", "42"), + ("Vary", "Origin"), + ]).0 + ); +}