Merge branch 'master' of https://git.cscherr.de/PlexSheep/rs-unsafe
Cargo Format, Check and Test / cargo fmt (push) Successful in 53s Details
Cargo Format, Check and Test / cargo check (push) Successful in 1m2s Details
Cargo Format, Check and Test / cargo test (push) Successful in 1m7s Details

This commit is contained in:
Christoph J. Scherr 2024-01-10 21:44:30 +01:00
commit 7c19e55d97
4 changed files with 14 additions and 15 deletions

View File

@ -5,7 +5,5 @@
fn main() { fn main() {
// Tell Cargo that if the given file changes, to rerun this build script. // Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo:rerun-if-changed=src/hello.c"); println!("cargo:rerun-if-changed=src/hello.c");
cc::Build::new() cc::Build::new().file("lib/test.c").compile("test");
.file("lib/test.c")
.compile("test");
} }

View File

@ -23,20 +23,18 @@ fn main() {
// confirmed safe inputs // confirmed safe inputs
let qux = unsafe { ret19() }; let qux = unsafe { ret19() };
println!("`ret19()` returned {qux}"); println!("`ret19()` returned {qux}");
let st = MyStruct { let st = MyStruct { foo: 17, bar: 0x41 };
foo: 17, bar: 0x41
};
// converting a c "string" to a real rust String is // converting a c "string" to a real rust String is
// a bit complicated // a bit complicated
let info: &str = unsafe { let info: &str = unsafe {
// convert the returned value to a rust internal CStr. // convert the returned value to a rust internal CStr.
std::ffi::CStr::from_ptr( std::ffi::CStr::from_ptr(
// the function returns a pointer to a array of chars on the heap // the function returns a pointer to a array of chars on the heap
structInfo(&st) structInfo(&st),
) )
// now to a string slice (result) // now to a string slice (result)
.to_str() .to_str()
.unwrap() .unwrap()
}; };
println!("{info}"); println!("{info}");
} }

View File

@ -1,4 +1,4 @@
use std::any::{TypeId, type_name}; use std::any::{type_name, TypeId};
//* # See what's behind the primitive datatypes of Rust //* # See what's behind the primitive datatypes of Rust
//* //*
//* This Crate shows off, how primitive Types of rust are stored in memory. //* This Crate shows off, how primitive Types of rust are stored in memory.
@ -26,8 +26,10 @@ macro_rules! investigate {
unsafe { unsafe {
for (index, item) in $v.iter().enumerate() { for (index, item) in $v.iter().enumerate() {
let pointer = item as *const $t; let pointer = item as *const $t;
let raw_pointer: [u8; std::mem::size_of::<$t>()] = std::mem::transmute(item.clone()); let raw_pointer: [u8; std::mem::size_of::<$t>()] =
println!("\t{index:02x}\titem:\t{item:?}\n\ std::mem::transmute(item.clone());
println!(
"\t{index:02x}\titem:\t{item:?}\n\
\t\tpointer: {:X?}\n\ \t\tpointer: {:X?}\n\
\t\talign: {:#X} B\n\ \t\talign: {:#X} B\n\
\t\tmemory: {:X?}\n", \t\tmemory: {:X?}\n",

View File

@ -1,6 +1,6 @@
fn main() { fn main() {
println!( println!(
"The dark power of unsafe rust awakens! "The dark power of unsafe rust awakens!
This is the default executable! It does not do much, use another executable. This is the default executable! It does not do much, use another executable.
@ -9,5 +9,6 @@ Select your target like this:
To see a list of all runnable binaries, you can use the following command. To see a list of all runnable binaries, you can use the following command.
`cargo run --bin` `cargo run --bin`
"); "
);
} }