Skip to content

add goal for Complex<T> (for FFI) - #747

Open
folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:interop-complex
Open

add goal for Complex<T> (for FFI)#747
folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:interop-complex

Conversation

@folkertdev

@folkertdev folkertdev commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Today there are still a number of cases where Rust cannot, in a portable way, express type signatures that C can define.

The C _Complex type is one such missing piece. This type is conceptually just

#[repr(C)]
struct Complex<T> { 
    real: T, 
    imaginary: T 
}

But many ABI pass Complex<{float}> and Complex<{int}> in a custom way, different from the equivalent C struct. The value of Complex<T> as a built-in type is that we can match those ABIs.

use core::ffi::c_double;

unsafe extern "C" {
    // Complex square root provided by libm.
    safe fn csqrtf(_: Complex<c_float>) -> Complex<c_float>;
}

fn main() {
    let c = Complex::new(-1.0, 0.0);
    assert_eq!(csqrtf(c), Complex::new(0.0, 1.0));
}

There is an accepted RFC (RFC 3892) for Complex<T>.

Rendered

@nxsaken nxsaken added the C-goal-proposal This is a goal proposal label Aug 3, 2026
@rustbot

rustbot commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

A zulip topic was opened to discuss this issue.

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

Labels

C-goal-proposal This is a goal proposal

Projects

Status: To Do

Development

Successfully merging this pull request may close these issues.

3 participants