build: update all crates to Rust 2024 edition#8449
Conversation
Largest change in the FFI crate.
With 2024 (but not 2021) edition unsafe code
inside unsafe functions should be marked separately
so we can mark exactly the code that is unsafe.
Some CFFI functions even have no unsafe code inside.
Most interesting change is that .strdup()
functions are not marked as unsafe anymore.
They are allocating memory and return raw pointers,
but there is nothing unsafe about it.
Only using the returned raw pointers is unsafe.
This way calls to .strdup() don't have to be marked
with unsafe{} blocks.
|
Maybe doing this earlier could have avoided some bugs we had in CFFI with e.g. spawning tasks with dangling pointers that compiler did not complain about because all the code was treated as unsafe. There are some formatting changes because rustfmt simply does less on old editions and nobody is going to improve rustfmt for old Rust. Will rebase after #8437 which removes some CFFI. |
| /// interior null byte as this can not be represented in raw C | ||
| /// strings. | ||
| unsafe fn strdup(&self) -> *mut libc::c_char; | ||
| fn strdup(&self) -> *mut libc::c_char; |
There was a problem hiding this comment.
This is likely the most interesting change. This should not be unsafe but all the callers were previously in implicit unsafe blocks.
| fn strdup(&self) -> *mut libc::c_char { | ||
| unsafe { | ||
| let tmp = CString::new_lossy(self); | ||
| dc_strdup(tmp.as_ptr()) |
There was a problem hiding this comment.
This is not really unsafe, we know the pointer is correct. Also should use https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_raw directy (which is safe) to make it clear that we are simply allocating CString and convert it to a raw pointer.
Largest change in the FFI crate.
With 2024 (but not 2021) edition unsafe code
inside unsafe functions should be marked separately so we can mark exactly the code that is unsafe.
Some CFFI functions even have no unsafe code inside.
Most interesting change is that .strdup()
functions are not marked as unsafe anymore.
They are allocating memory and return raw pointers, but there is nothing unsafe about it.
Only using the returned raw pointers is unsafe.
This way calls to .strdup() don't have to be marked with unsafe{} blocks.