Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'pub const BAR: &str = "hello world";' #324

Closed
AxelNennker opened this issue Apr 19, 2019 · 3 comments
Closed

Support 'pub const BAR: &str = "hello world";' #324

AxelNennker opened this issue Apr 19, 2019 · 3 comments

Comments

@AxelNennker
Copy link
Contributor

currently cbindgen does not support const &st
pub const BAR: &'static str = "hello world";

ignisvulpis@namenlos:~/development/cbindgen$ cbindgen -o /tmp/x.x tests/rust/constant.rs 
WARN: Skip constant::BAR - (Unhanded const definition)
ignisvulpis@namenlos:~/development/cbindgen$ 

I think that the bindings should be
#define BAR L"hello world" for C
and
static const wchar_t* BAR = L"hello world"; for C++

@eqrion
Copy link
Collaborator

eqrion commented Apr 19, 2019

Rust &'static str is not equivalent to C/C++ string literals. Rust string slices are represented as a (ptr, len). The ptr is to non-null terminated UTF-8 encoded characters. C/C++ string literals are just a ptr to a null-terminated ASCII or UTF-32 string.

It's possible to create constants on the C/C++ side that are similar to the Rust constants, but they wouldn't be able to passed over FFI unless they were wrapped somehow.

What do you need this functionality for?

@AxelNennker
Copy link
Contributor Author

my pet project is https://github.com/hyperledger/indy-sdk/tree/master/libindy and when I run cbindgen there it produces a ton of warnings.
The project has a shared library with an C interface.
Which is here: https://github.com/hyperledger/indy-sdk/tree/master/libindy/src/api
Currently the C bindings for it were manually created. They are here:
https://github.com/hyperledger/indy-sdk/tree/master/libindy/include

I would like to produce them with cbindgen.

The constants are currently not exported but I think the should. Everything that comes through the API from C to Rust are C null terminated C 'strings'. The top API layer handles them.
https://github.com/hyperledger/indy-sdk/blob/master/libindy/src/utils/inmem_wallet.rs#L30
Some are de-serialized from JSON others are sent over the network. Currently users of the library create those constants manually. I want cbindgen to create them. The wrappers for different languages are here:
https://github.com/hyperledger/indy-sdk/tree/master/wrappers

This is the use case. I am not there where I want this to be.

@rlidwka
Copy link

rlidwka commented Feb 10, 2024

Rust &'static str is not equivalent to C/C++ string literals.

Rust CStr is equivalent to C/C++ string literals.

So I've just opened an issue asking for that: #927

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

No branches or pull requests

3 participants