Skip to content

build: update all crates to Rust 2024 edition#8449

Draft
link2xt wants to merge 1 commit into
mainfrom
link2xt/rust-edition-2024
Draft

build: update all crates to Rust 2024 edition#8449
link2xt wants to merge 1 commit into
mainfrom
link2xt/rust-edition-2024

Conversation

@link2xt

@link2xt link2xt commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

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.

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.
@link2xt

link2xt commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator Author

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;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant