From 6460be62c31d0413cbcaea8ed7856632b6167ac0 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 14 Dec 2017 18:09:45 +0700 Subject: [PATCH] Use '#[repr(C)]' on 'str' wrappers to guarantee correct layout. Closes #494. --- lib/src/http/raw_str.rs | 3 ++- lib/src/http/uncased.rs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/http/raw_str.rs b/lib/src/http/raw_str.rs index 652adbb5..a4828963 100644 --- a/lib/src/http/raw_str.rs +++ b/lib/src/http/raw_str.rs @@ -46,6 +46,7 @@ use http::uncased::UncasedStr; /// /// [`FromParam`]: /rocket/request/trait.FromParam.html /// [`FromFormValue`]: /rocket/request/trait.FromFormValue.html +#[repr(C)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct RawStr(str); @@ -276,7 +277,7 @@ impl RawStr { impl<'a> From<&'a str> for &'a RawStr { #[inline(always)] fn from(string: &'a str) -> &'a RawStr { - unsafe { ::std::mem::transmute(string) } + unsafe { &*(string as *const str as *const RawStr) } } } diff --git a/lib/src/http/uncased.rs b/lib/src/http/uncased.rs index d8ac4c38..5cfb76ab 100644 --- a/lib/src/http/uncased.rs +++ b/lib/src/http/uncased.rs @@ -18,6 +18,7 @@ use std::fmt; /// /// let ascii_ref: &UncasedStr = "Hello, world!".into(); /// ``` +#[repr(C)] #[derive(Debug)] pub struct UncasedStr(str);