rtt-target
rtt-target copied to clipboard
Eliminate some simple unsafe operations
Replace pointer methods with slice copies and a transmute with into
Did you test this on target? :)
This week I have no target with me :(
I did do a unit tests https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=80b5d3b9bc4063e43f01b4adf4269707 but testing on hardware would be better of course
I think the ptr::copy_nonoverlapping
was meant to try to persuade the compiler to never optimize the writes in any way, but I guess the real way to do that would be to use ptr::write_volatile
for that (too bad we still don't have volatile_copy_nonoverlapping_memory
)
Hm, does the fence not provide sufficient guarantees there?
A volatile memcpy would work (with the bonus of zip being limited to the shorter of the two buffer lengths):
const ID_VAL: &[u8] = b"SEGGER RTT";
debug_assert!(ID_VAL.len() <= id.len());
for (src, dest) in ID_VAL.iter().zip(id.iter_mut()) {
unsafe { ptr::write_volatile(dest, *src) };
}
copy_from_slice
is just a wrapper around copy_nonoverlapping
fwiw
https://github.com/rust-lang/rust/blob/f63ccaf25f74151a5d8ce057904cd944074b01d2/library/core/src/slice/mod.rs#L3354-L3356