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() {
// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo:rerun-if-changed=src/hello.c");
cc::Build::new()
.file("lib/test.c")
.compile("test");
cc::Build::new().file("lib/test.c").compile("test");
}

View File

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

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
//*
//* This Crate shows off, how primitive Types of rust are stored in memory.
@ -26,8 +26,10 @@ macro_rules! investigate {
unsafe {
for (index, item) in $v.iter().enumerate() {
let pointer = item as *const $t;
let raw_pointer: [u8; std::mem::size_of::<$t>()] = std::mem::transmute(item.clone());
println!("\t{index:02x}\titem:\t{item:?}\n\
let raw_pointer: [u8; std::mem::size_of::<$t>()] =
std::mem::transmute(item.clone());
println!(
"\t{index:02x}\titem:\t{item:?}\n\
\t\tpointer: {:X?}\n\
\t\talign: {:#X} B\n\
\t\tmemory: {:X?}\n",

View File

@ -1,6 +1,6 @@
fn main() {
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.
@ -9,5 +9,6 @@ Select your target like this:
To see a list of all runnable binaries, you can use the following command.
`cargo run --bin`
");
"
);
}