-
Notifications
You must be signed in to change notification settings - Fork 37
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
Use of C strings in API [was: this project vs couchbase-lite-core C API?] #22
Comments
Interesting issue! I works on Rust bindings for this project https://github.com/jeromebenois/couchbase-lite-c-rs It seems the C API in couchbase-lite-core in an internal API and maybe change. It explains here: https://blog.couchbase.com/couchbase-lite-in-c/ I think that we must talk together. It’s a shame to have 2 rust bindings. |
But it has little sense for me. You need any way build couchbase-lite-core for Rust binding, so you stick to specific version of couchbase-lite-core any way. So any API changes doesn't matter until you going upgrade to new couchbase-lite-core and during upgrade you need any way test many things so adding new arguments to call and so on doesn't matter.
Definitely. I make my binding open-source because of I need collaborators. |
The short answer is that Couchbase has customers that want to use Couchbase Lite directly from C/C++, or in environments where neither Java/.NET/Cocoa are an option. So we're building a version for them to use, with an API that's pretty close to our other platform APIs. Couchbase Lite Core is an internal library with an internal API. We reserve the right to change it around drastically at any moment. It's possible it will go away entirely and be merged into CBL-C. The API is pretty complicated to figure out in some areas. And there are annoying problems building it, that we don't have an urgent reason to fix, but that some new developers run into. We used to be more open about other devs using LiteCore, because we had no better alternative to offer. Now we do. |
Performance-sensitive APIs in CBL-C, like at the document property level, work with slices. The lowest level thing I can think of that uses C strings is getting documents, but the overhead of allocating a string isn't going to be much compared to reading the data out of SQLite. |
But why mix C null terminated strings and slices? It is understandable if all your API will works with null terminated strings, |
Good question. I started out wanting to make the API more idiomatic in C. But when it got down to the document property level, it's really just exposing the Fleece API. Wrapping that whole API in something that exposed C strings instead would be both awkward and introduce a lot of performance overhead due to copying. So at this point you may be right that it's best to go for consistency and make the higher level APIs slice-based too. I'm not in charge of CBL API decisions; @pasin, what do you think? |
I think we should do what makes sense to the platform. Can we have some examples about the proposed change? |
The proposal is to change strings in the public API from CBLDatabase* CBLDatabase_Open(const char *name _cbl_nonnull,
const CBLDatabaseConfiguration* config,
CBLError* error) CBLAPI; Here The argument for However, as @Dushistov points out, a second category of clients is bindings to other languages (Rust, Python, JS, Go, etc.) In many cases these languages store strings as UTF-8 but don't null-terminate them, which means that passing a string into one of these CBL-C APIs requires first copying the string into a temp buffer and appending a zero byte. (And handling a returned C string involves calling Another argument against I never did push The main effect on C clients is learning what |
It doesn't make that much difference to C# since both of them will need some marshalling work (C# strings are internally stored as UTF-16) but I agree we should be consistent which looks like it means using slices everywhere. |
I recently added alternative versions of the API functions that take |
@Dushistov FYI, I've added a preliminary 'official' Rust binding in the |
|
Only bindings.rs is auto-generated (at build time) from the C headers. That ensures we have accurate C bindings. Those are private; the public classes are wrappers around them.
I don't understand that paragraph. Are you saying that the Rust build script should invoke CMake to build the native library? That sounds like a good idea, but it wasn't necessary for bringup so I haven't done it yet. |
Rust ABI is intentionally not stable, you can not give pre-build crate to user for linking, user have to build your crate from source code. You obviously can document that user have to compile C++ code and only after that it can build Rust code, but much more convenient if C++ will build as part of Rust build. |
As I said, it's a good idea :) it just wasn't on the critical path to getting something running. We don't want to require it, though. Couchbase distributes Couchbase Lite to customers in binary form (so that our support team knows exactly what bits the customer has), and the commercial Enterprise Edition contains some closed-source bits so it's only available in binary form. So we need to preserve the ability to build the Rust library from existing CBL headers and dylib. |
Oh, also
I don't think it's intentional anymore. There is talk of stabilizing the ABI (I think there have been posts about it on the Rust blog.) Being able to distribute libraries in binary form is useful — before Swift finalized its ABI we kept having to provide new builds of CBL-Swift every time a new version of Xcode came out. |
There are several topics in https://internals.rust-lang.org/ , but there are no RFC as I know. And you should note that there are no option in |
I am really confused by this project.
There is https://github.com/couchbase/couchbase-lite-core with C API.
What for this project?
Recently I make my Rust binding for couchbase-lite-core public
https://github.com/Dushistov/couchbase-lite-rust/ it is "very alpha" though I use it in my project.
Should I switch to couchbase-lite-C?
From the first look couchbase-lite-C API is worse in compare with couchbase-lite-core for Rust.
Rust native work with slices, so to pass null terminated string you have to allocate memory.
While couchbase-lite-core C API works with slices that maps one to one for all fundamental Rust types.
cc @snej
The text was updated successfully, but these errors were encountered: