struct stats_p {
unsigned long pass;
unsigned long drop;
};
and I kinda need to know how to pass that in those functions&[u8]
&[u8]
let stats = Stats::default();
let stats_slice = stats as &[u8];
?use std::slice;
use std::mem;
unsafe fn as_bytes<T>(t: &T) -> &[u8] {
slice::from_raw_parts(t as *const T as *const u8, mem::size_of_val(&t))
}
Forms a slice from a pointer and a length.
T
of slice::from_raw_parts
is u8
) (edited)*const u8
and use the result as &[u8]
)T: Sized
is implied, if you don't want that, you need T: ?Sized
, I thinkmem::size_of
insteaduse std::mem;
use std::slice;
unsafe fn as_bytes<T>(t: &T) -> &[u8] {
slice::from_raw_parts(t as *const T as *const u8, mem::size_of::<T>())
}
&T
, not T
#[repr(C)]
on structs you use this onstruct Stats {
pass: u64,
drop: u64,
}
use std::slice;
use std::mem;
unsafe fn as_bytes<T>(t: &T) -> &[u8] {
slice::from_raw_parts(t as *const T as *const u8, mem::size_of::<T>())
}
fn main() {
let stats = Stats { pass: 5, drop: 5 };
let stats_slice: &[u8] = unsafe {
as_bytes::<Stats>(&stats)
};
for element in stats_slice.iter() {
println!("{element}");
}
}
but doesn't seem to print anything5
0
0
0
0
0
0
0
5
0
0
0
0
0
0
0
for me @ReiTW#[repr(C)]
to your Stats
struct) let stats_slice: &[u8] = unsafe {
as_bytes::<Stats>(&stats)
};
let stats_slice = unsafe {
as_bytes(&stats)
};
let stats_slice = unsafe { as_bytes(&stats) };
exec
to be used in the script?c++
try {
ReadDiscord(); // may throw Ping
} catch(CPing& Ping) {
HandlePing(Ping);
}
(edited)