Created
February 15, 2016 08:36
-
-
Save tomaka/b579e276b751c42dc3df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate libc; | |
| use std::os::raw::c_char; | |
| use std::mem; | |
| use std::slice; | |
| pub struct CStr { | |
| inner: [c_char] | |
| } | |
| impl CStr { | |
| #[inline(never)] | |
| pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr { | |
| let len = libc::strlen(ptr); | |
| mem::transmute(slice::from_raw_parts(ptr, len as usize + 1)) | |
| } | |
| } | |
| #[inline(never)] | |
| pub fn hello() -> *const u8 { | |
| unsafe { | |
| let f = CStr::from_ptr(0xdeadbeefusize as *const _); | |
| let v: (*const u8, usize) = mem::transmute(f); | |
| v.0 | |
| } | |
| } | |
| pub fn main() { | |
| println!("{:?}", hello()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment