From 8c2e4352988f08090ef4a3760b4da912bd141d59 Mon Sep 17 00:00:00 2001 From: Daiki Mizukami Date: Thu, 3 Aug 2017 21:37:28 +0900 Subject: [PATCH] Add 'Box' conversion methods to 'UncasedStr'. --- lib/src/http/uncased.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/src/http/uncased.rs b/lib/src/http/uncased.rs index 12d24fdd..e921254b 100644 --- a/lib/src/http/uncased.rs +++ b/lib/src/http/uncased.rs @@ -55,6 +55,25 @@ impl UncasedStr { pub fn as_str(&self) -> &str { &self.0 } + + /// Converts a `Box` into an `Uncased` without copying or allocating. + /// + /// # Example + /// + /// ```rust + /// use rocket::http::uncased::Uncased; + /// + /// let uncased = Uncased::new("Hello!"); + /// let boxed = uncased.clone().into_boxed_uncased(); + /// assert_eq!(boxed.into_uncased(), uncased); + /// ``` + #[inline(always)] + pub fn into_uncased(self: Box) -> Uncased<'static> { + unsafe { + let raw_str = Box::into_raw(self) as *mut str; + Uncased::from(Box::from_raw(raw_str).into_string()) + } + } } impl PartialEq for UncasedStr { @@ -172,6 +191,24 @@ impl<'s> Uncased<'s> { self.string.into_owned() } + /// Converts `self` into a `Box`. + /// + /// # Example + /// + /// ```rust + /// use rocket::http::uncased::Uncased; + /// + /// let boxed = Uncased::new("Content-Type").into_boxed_uncased(); + /// assert_eq!(&*boxed, "content-type"); + /// ``` + #[inline(always)] + pub fn into_boxed_uncased(self) -> Box { + unsafe { + let raw_str = Box::into_raw(self.string.into_owned().into_boxed_str()); + Box::from_raw(raw_str as *mut UncasedStr) + } + } + /// Returns the inner `Cow`. #[doc(hidden)] #[inline(always)]