Skip to content

Instantly share code, notes, and snippets.

@uzytkownik
Created January 5, 2014 21:18
Show Gist options
  • Save uzytkownik/8274049 to your computer and use it in GitHub Desktop.
Save uzytkownik/8274049 to your computer and use it in GitHub Desktop.
Casting unsafe pointer to struct containing borrowed pointer
struct Test<'t> {
i: &'t int
}
unsafe fn cast<'t>(test: &'t *mut std::libc::c_void) -> &'t mut Test<'t> {
&*(test.clone() as *mut Test<'t>)
}
pub fn main() {
unsafe {
let i = 1;
let mut t = Test{i: &i};
let ptr = std::ptr::to_mut_unsafe_ptr(&t) as *mut std::libc::c_void;
let c = cast(&ptr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment