Compare commits

...

6 Commits

Author SHA1 Message Date
Christoph J. Scherr 7c19e55d97 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
2024-01-10 21:44:30 +01:00
Christoph J. Scherr 030df9fe7e renaming some CI 2024-01-10 21:44:28 +01:00
PlexSheep 8f237ea40e Automatical formatting 2024-01-10 20:38:30 +00:00
Christoph J. Scherr 18fcdc6e59 just testing the CI/CD
Cargo Format, Check and Test / cargo test (push) Successful in 52s Details
2024-01-10 21:37:21 +01:00
Christoph J. Scherr f9baf1890b add cargo action 2024-01-10 21:35:57 +01:00
Christoph J. Scherr f7e0b277ce dumping with macro 2023-09-20 11:30:50 +02:00
5 changed files with 72 additions and 31 deletions

View File

@ -0,0 +1,32 @@
name: Cargo Format, Check and Test
on: [push, pull_request]
jobs:
format:
name: cargo fmt
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: rustup component add rustfmt
- run: cargo fmt
- uses: stefanzweifel/git-auto-commit-action@v5
with:
# Optional. Commit message for the created commit.
# Defaults to "Apply automatic changes"
commit_message: Automatical formatting
check:
name: cargo check
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo check --all-features --verbose
test:
name: cargo test
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all-features --verbose

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,20 +23,18 @@ 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()
.unwrap()
// now to a string slice (result)
.to_str()
.unwrap()
};
println!("{info}");
}

View File

@ -1,6 +1,4 @@
use std::any::{TypeId, type_name};
use std::mem::*;
use std::fmt::Debug;
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.
@ -9,23 +7,37 @@ fn main() {
let mut items = Vec::new();
items.push(true);
items.push(false);
dump_type::<bool>(items);
investigate!(bool, items);
let mut items = Vec::new();
items.push(String::from("foo"));
items.push(String::from("bar"));
items.push(String::from("文学"));
items.push(String::from("qux"));
investigate!(String, items);
}
fn dump_type<T: Debug + 'static>(items: Vec<T>) {
println!("Type:\t{}", type_name::<T>());
println!("\tID:\t{:?}", TypeId::of::<T>());
println!("\tItems:");
unsafe {
for (index, item) in items.iter().enumerate() {
let pointer = item as *const T;
let raw_pointer = pointer as *const u8;
println!("\t{index:02x}\titem:\t{item:?}\n\
\t\tpointer: {:X?}\n\
\t\tmemory: {:X?}",
pointer,
*raw_pointer,
);
#[macro_export]
macro_rules! investigate {
($t:ty, $v:tt) => {
println!("Type:\t{}", type_name::<$t>());
println!("\tID:\t{:?}", TypeId::of::<$t>());
println!("\tItems:");
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\
\t\tpointer: {:X?}\n\
\t\talign: {:#X} B\n\
\t\tmemory: {:X?}\n",
pointer,
std::mem::align_of::<$t>(),
raw_pointer,
);
}
}
}
};
}

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`
");
"
);
}