feat(bindings/C): opendal_operator_ptr construction using kvs#2329
feat(bindings/C): opendal_operator_ptr construction using kvs#2329Xuanwo merged 4 commits intoapache:mainfrom
Conversation
|
Related #2321 |
|
How about API like the following: struct opendal_operator_builder *options = opendal_operator_builder_new();
opendal_operator_builder_set(options, "a", "x");
opendal_operator_builder_set(options, "b", "y");
opendal_operator_builder_set(options, "c", "z");
opendal_operator_ptr ptr = opendal_operator_new(options);Can we implement the |
IMO the builder is different from this map approach, I think For the move semantic, it is not a common approach in C, usually if user manually allocates a heap memory, the user has to free it himself. (If we free the heap memory in our function, a programmer who often programs in C would probably double free the memory). |
LGTM!
Ok, let's add |
6b054f7 to
581bb42
Compare
| ```C | ||
| opendal_operator_options options = opendal_operator_options_new(); | ||
| opendal_operator_options_set(&options, "root", "/myroot"); | ||
|
|
||
| // .. use your opendal_operator_options | ||
|
|
||
| opendal_operator_options_free(options); | ||
| ``` |
There was a problem hiding this comment.
In this line, it passes wrong type to opendal_operator_options_free. It should be opendal_operator_options_free(&options);
Moreover, opendal_operator_options_new returns a value, but set and free requires a pointer. It seems easy to make mistakes here. Maybe it's better to unify the types.
There was a problem hiding this comment.
I originally wanted to unify them into pointers, by #[repr(transparent)], but this will cause the doxygen not recognizing the header cbindgen generated.
I agree that this is a problem and we should fix this, hence I have created an issue #2462 here.
FYI I am sort of being covered by a hell lot of mess these days 😢 , so it may take a while but not too long.
Introduce initializing opendal_operator by key-value pairs. With this PR, we can build operator on any scheme theoretically 😆 .
Some explainations
For map construction, I came up with essentially three ideas.
opendal_mapstruct, which is essentially a pointer to Rust Hashmap. I did not use this because this introduce complexity just like we construct aopendal_operator_ptr, users have to pay their extra attention on freeing the heap memory that is even used only once.opendal_operator_ptr_from_kvs(), this api would be likeThe pro of this is that we do not need to introduce any new foreign, hard to manage data structures into Rust (like we actually do in this PR for
opendal_kv).The con of this is that users have we make sure that the ordering and the number of the provided keys and values are correct, making this API really bad.
opendal_kv, which is essentially a simple key-value pair. This helps us to easier align the keys and values provided by user, and provide a more friendly interface.We do not need to explicitly manage heap allocation of hashmap, manage the correctness of the ordering of key-values, and we can make use of this kv data structure in the future.