Skip to content

Commit

Permalink
Use '#[repr(C)]' on 'str' wrappers to guarantee correct layout.
Browse files Browse the repository at this point in the history
Closes #494.
  • Loading branch information
SergioBenitez committed Dec 14, 2017
1 parent 4ea9b04 commit 5922eb5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/src/http/raw_str.rs
Expand Up @@ -44,6 +44,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);

Expand Down Expand Up @@ -270,7 +271,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) }
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/http/uncased.rs
Expand Up @@ -18,6 +18,7 @@ use std::fmt;
///
/// let ascii_ref: &UncasedStr = "Hello, world!".into();
/// ```
#[repr(C)]
#[derive(Debug)]
pub struct UncasedStr(str);

Expand Down

0 comments on commit 5922eb5

Please sign in to comment.