-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
FFI mechanism to declare a symbol for an array #54450
Copy link
Copy link
Open
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-arrayArea: `[T; N]`Area: `[T; N]`C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-arrayArea: `[T; N]`Area: `[T; N]`C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
If I have a C declaration
I can declare that symbol in Rust as
But if I have a C declaration
I can't directly declare that in Rust in an immediately usable way. In this case,
symbolrefers directly to the array of characters; while in C it will "degrade" to a pointer if used in a context that expects a pointer, in Rust a declaration referring tosymbolwill refer to the characters directly. So, for instance, declaring it as a*c_charwill result in a pointer whose numeric value contains the firstsizeof::<*c_char>()bytes of"hello world".Declaring
symbolas a[c_char; 0]and then obtaining and using its pointer seems wrong.I can think of a few useful ways to do this, one more straightforward than the other.
One would be to have a means of defining in an
extern "C"block something that gets the value of the address of the symbol, just as if in C I'd writtenchar *symbol_ptr = symbol;and then referenced that from Rust. That seems easy enough to do, modulo bikeshedding over the syntax to do so.Another would be to define
symbolas a C array of unknown length. However, that seems unfortunate to deal with.The most ideal approach I can think of would be to define
symbolas a[c_char; _](using the elided size syntax from rust-lang/rfcs#2545), and then infer the size from the symbol size:I don't know how feasible that would be, but it'd be incredibly convenient.