Use '#[repr(C)]' on 'str' wrappers to guarantee correct layout.

Closes #494.
This commit is contained in:
Sergio Benitez 2017-12-14 18:09:45 +07:00
parent 94c7a1997e
commit 6460be62c3
2 changed files with 3 additions and 1 deletions

View File

@ -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) }
}
}

View File

@ -18,6 +18,7 @@ use std::fmt;
///
/// let ascii_ref: &UncasedStr = "Hello, world!".into();
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct UncasedStr(str);