You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
46 lines
1.1 KiB
// tests6.rs
|
|
//
|
|
// In this example we take a shallow dive into the Rust standard library's
|
|
// unsafe functions. Fix all the question marks and todos to make the test
|
|
// pass.
|
|
//
|
|
// Execute `rustlings hint tests6` or use the `hint` watch subcommand for a
|
|
// hint.
|
|
|
|
// I AM NOT DONE
|
|
|
|
struct Foo {
|
|
a: u128,
|
|
b: Option<String>,
|
|
}
|
|
|
|
/// # Safety
|
|
///
|
|
/// The `ptr` must contain an owned box of `Foo`.
|
|
unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box<Foo> {
|
|
// SAFETY: The `ptr` contains an owned box of `Foo` by contract. We
|
|
// simply reconstruct the box from that pointer.
|
|
let mut ret: Box<Foo> = unsafe { ??? };
|
|
todo!("The rest of the code goes here")
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::time::Instant;
|
|
|
|
#[test]
|
|
fn test_success() {
|
|
let data = Box::new(Foo { a: 1, b: None });
|
|
|
|
let ptr_1 = &data.a as *const u128 as usize;
|
|
// SAFETY: We pass an owned box of `Foo`.
|
|
let ret = unsafe { raw_pointer_to_box(Box::into_raw(data)) };
|
|
|
|
let ptr_2 = &ret.a as *const u128 as usize;
|
|
|
|
assert!(ptr_1 == ptr_2);
|
|
assert!(ret.b == Some("hello".to_owned()));
|
|
}
|
|
}
|