feat(rust): implement rust sled demo - #450
Conversation
|
please write a README file like this: https://github.com/goplus/llgo/blob/main/c/sqlite/README.md |
ecfbe8a to
d1e5db7
Compare
| } | ||
|
|
||
| //llgo:link (*SledConfig).OpenDB C.sled_open_db | ||
| func (conf *SledConfig) OpenDB() *SledDb { |
There was a problem hiding this comment.
It's not common to add OpenDB in a Config type.
//go:linkname Open C.sled_open_db
func Open(conf *Config) *DBTo be confirmed: how sled_open_db indicates an open error?
There was a problem hiding this comment.
It's not common to add OpenDB in a
Configtype.//go:linkname Open C.sled_open_db func Open(conf *Config) *DBTo be confirmed: how
sled_open_dbindicates an open error?
In the implementation of the C wrapper in Sled, it directly calls unwrap to throw an error when there is an open error. For this C wrapper implementation, it indeed doesn't feel elegant. I think returning a null pointer for an open error might be a more elegant handling method.
/// Open a sled lock-free log-structured tree. Consumes the passed-in config.
#[no_mangle]
pub unsafe extern "C" fn sled_open_db(config: *mut Config) -> *mut Db {
let config = Box::from_raw(config);
Box::into_raw(Box::new(config.open().unwrap()))
}When I call this function in C and pass in a null pointer, it directly throws an error:
Full Demo https://github.com/luoliwoshang/RustTest/blob/main/sled/test/test.c
printf("Opening database...\n");
free(config); // mock error
void *db = sled_open_db(config);
printf("Database opened: %p\n", db);
if (db == NULL)
{
printf("Failed to open database\n");
sled_free_config(config);
return 1;
}./test
Creating config...
Config created: 0x6000031e4010
Setting path...
Path set, new config: 0x6000031e4020
Opening database...
[1] 6955 segmentation fault ./test
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #450 +/- ##
=======================================
Coverage 97.51% 97.51%
=======================================
Files 19 19
Lines 4420 4420
=======================================
Hits 4310 4310
Misses 94 94
Partials 16 16 ☔ View full report in Codecov by Sentry. |
Signed-off-by: hackerchai <i@hackerchai.com>
Signed-off-by: hackerchai <i@hackerchai.com>
d1e5db7 to
a36d5b6
Compare
llgo:rust:sled:a working demo
Signed-off-by: hackerchai i@hackerchai.com