-
Notifications
You must be signed in to change notification settings - Fork 2
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
Rework ffi with new logging
, error handling
and some improvements in arch and safe
#10
base: main
Are you sure you want to change the base?
Changes from 7 commits
7333525
4d59bae
e0edd83
e8691c2
63be554
b1a3d77
f4c7cb4
a40e21e
7dd3cd9
4600061
4f6b95b
a11188f
edd0b87
646f1ae
6d7c353
abee06b
8420a12
4ec685a
6c71760
cc2eee1
2c10ab3
fdc90c9
f48486c
b7e8a25
7454ec0
d54db5d
fba6e78
d5025c0
0890293
56ae6bc
7061c37
2de822a
0f4d4fa
9c58bb0
f570601
a2ba506
4752904
18ed088
6a7a90f
a57cec3
67afd89
367b05a
74fbc82
804823b
056c7e2
4adab77
6b1345a
3338ba8
78cf083
97c2640
918d255
26927a7
8561b07
903d07e
ae182b5
3eb54ef
d6de95a
c2a4813
766df15
2e7e592
155f475
d619560
af3fef0
722094c
8da33d5
3bf0a7e
0e71c0e
8433376
a5156ed
a95d580
fb70b9c
9042ce8
3754990
3b272d9
fb2aad6
ce9449a
97e12b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "doublets-ffi-examples" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
doublets-ffi = { path = "../../../doublets-ffi" } | ||
doublets = { path = "../../../doublets" } | ||
tracing = { version = "0.1.36" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use doublets_ffi::{ | ||
export::{doublets_create_log_handle, doublets_free_log_handle}, | ||
FFICallbackContext, | ||
}; | ||
use std::{ | ||
ffi::{c_char, CStr, CString}, | ||
ptr, | ||
}; | ||
|
||
unsafe extern "C" fn callback(_: FFICallbackContext, ptr: *const c_char) { | ||
let cstr = CStr::from_ptr(ptr); | ||
print!("{}", cstr.to_str().unwrap()); | ||
} | ||
|
||
fn main() { | ||
let ctx = ptr::null_mut(); | ||
let level = CString::new("trace").unwrap(); | ||
let use_ansi = true; | ||
let use_json = false; | ||
unsafe { | ||
let handle = doublets_create_log_handle(ctx, callback, level.as_ptr(), use_ansi, use_json); | ||
|
||
tracing::error!("SOMETHING IS SERIOUSLY WRONG!!!"); | ||
tracing::warn!("important informational messages; might indicate an error"); | ||
tracing::info!("general informational messages relevant to users"); | ||
tracing::debug!("diagnostics used for internal debugging of a library or application"); | ||
tracing::trace!("very verbose diagnostic events"); | ||
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move logs out |
||
|
||
doublets_free_log_handle(handle); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||||||||||||
use doublets::{ | ||||||||||||||||||
data::{Flow, LinksConstants}, | ||||||||||||||||||
Link, | ||||||||||||||||||
}; | ||||||||||||||||||
use doublets_ffi::{ | ||||||||||||||||||
constants::Constants, | ||||||||||||||||||
errors::DoubletsErrorKind, | ||||||||||||||||||
export::{doublets_create_log_handle, doublets_free_log_handle}, | ||||||||||||||||||
store::{create, doublets_constants_u64, doublets_create_united_store_u64}, | ||||||||||||||||||
FFICallbackContext, | ||||||||||||||||||
}; | ||||||||||||||||||
use std::{ | ||||||||||||||||||
ffi::{c_char, c_void, CStr, CString}, | ||||||||||||||||||
fs, | ||||||||||||||||||
ptr::{null, null_mut}, | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
unsafe extern "C" fn callback(_: FFICallbackContext, ptr: *const c_char) { | ||||||||||||||||||
print!("{}", CStr::from_ptr(ptr).to_str().unwrap()); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
extern "C" fn create_cb<F>(ctx: FFICallbackContext, before: Link<u64>, after: Link<u64>) -> Flow | ||||||||||||||||||
where | ||||||||||||||||||
F: FnMut(Link<u64>, Link<u64>), | ||||||||||||||||||
{ | ||||||||||||||||||
unsafe { | ||||||||||||||||||
let handler = &mut *(ctx as *mut F); | ||||||||||||||||||
(*handler)(before, after); | ||||||||||||||||||
Flow::Continue | ||||||||||||||||||
} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
unsafe fn magic_create<F>(ptr: *mut c_void, handler: F) | ||||||||||||||||||
where | ||||||||||||||||||
F: FnMut(Link<u64>, Link<u64>), | ||||||||||||||||||
{ | ||||||||||||||||||
let ctx = &mut (ptr, handler); | ||||||||||||||||||
let _ = create(ptr, null(), 0, ctx as *mut _ as *mut _, create_cb::<F>); | ||||||||||||||||||
} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could supplement this example almost to the level of real use |
||||||||||||||||||
|
||||||||||||||||||
fn main() { | ||||||||||||||||||
let level = CString::new("trace").unwrap(); | ||||||||||||||||||
unsafe { | ||||||||||||||||||
let handle = doublets_create_log_handle(null_mut(), callback, level.as_ptr(), true, false); | ||||||||||||||||||
|
||||||||||||||||||
let path = CString::new("doublets.links").unwrap(); | ||||||||||||||||||
let mut store = doublets_create_united_store_u64( | ||||||||||||||||||
path.as_ptr(), | ||||||||||||||||||
Constants::from(LinksConstants::external()), | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add ffi functions to create |
||||||||||||||||||
); | ||||||||||||||||||
|
||||||||||||||||||
magic_create(store.assume() as *mut _ as *mut _, |before, after| { | ||||||||||||||||||
print!("{before:?}\n{after:?}\n"); | ||||||||||||||||||
}); | ||||||||||||||||||
|
||||||||||||||||||
doublets_free_log_handle(handle); | ||||||||||||||||||
} | ||||||||||||||||||
let _ = fs::remove_file("doublets.links"); | ||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use doublets::{ | ||
data::{Flow, LinksConstants}, | ||
Link, | ||
}; | ||
use doublets_ffi::{ | ||
constants::Constants, | ||
errors::{ | ||
doublets_read_error, doublets_read_error_as_not_found, doublets_read_error_message, | ||
DoubletsErrorKind, | ||
}, | ||
export::{doublets_create_log_handle, doublets_free_log_handle}, | ||
store::{constants_for_store, create_unit_store, delete, free_store}, | ||
FFICallbackContext, | ||
}; | ||
use std::{ | ||
ffi::{c_char, CStr, CString}, | ||
fs, | ||
ptr::{null, null_mut}, | ||
}; | ||
|
||
unsafe extern "C" fn callback(_: FFICallbackContext, ptr: *const c_char) { | ||
print!("{}", CStr::from_ptr(ptr).to_str().unwrap()); | ||
} | ||
|
||
extern "C" fn create_cb(_: FFICallbackContext, _: Link<u64>, _: Link<u64>) -> Flow { | ||
Flow::Continue | ||
} | ||
|
||
fn main() { | ||
let level = CString::new("trace").unwrap(); | ||
unsafe { | ||
let handle = doublets_create_log_handle(null_mut(), callback, level.as_ptr(), true, false); | ||
|
||
let path = CString::new("doublets.links").unwrap(); | ||
let mut store = | ||
create_unit_store::<u64>(path.as_ptr(), Constants::from(LinksConstants::external())); | ||
|
||
let ptr = store.assume() as *mut _ as *mut _; | ||
|
||
let any = constants_for_store::<u64>(ptr).any; | ||
|
||
let query = [1 /* not exists index */, any, any]; | ||
let result = delete::<u64>(ptr, query.as_ptr(), 3, null_mut(), create_cb); | ||
|
||
if result as u8 != DoubletsErrorKind::None as u8 { | ||
let memchr = |buf: &[u8]| buf.iter().position(|x| *x == 0).unwrap(); | ||
|
||
// last error - DON'T USE PTR AFTER NEW DOUBLETS OPERATION | ||
let err = doublets_read_error(); | ||
let mut msg_buf = vec![0u8; 256]; | ||
|
||
doublets_read_error_message(msg_buf.as_mut_ptr().cast(), 256, err); | ||
|
||
msg_buf.drain(memchr(&msg_buf) + 1..); | ||
|
||
let cstring = CString::from_vec_with_nul(msg_buf).unwrap(); | ||
let str = cstring.to_str().unwrap(); | ||
tracing::error!("{}", str); | ||
|
||
// forget `err` ptr - we not in manage it deallocation | ||
let not_exists = doublets_read_error_as_not_found(err); | ||
tracing::error!("duplication: link {} does not exists", not_exists); | ||
|
||
// forget `err` ptr - we not in manage it deallocation | ||
} | ||
|
||
free_store::<u64>(ptr); | ||
|
||
doublets_free_log_handle(handle); | ||
} | ||
let _ = fs::remove_file("doublets.links"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use doublets_ffi::{ | ||
export::{doublets_create_log_handle, doublets_free_log_handle}, | ||
FFICallbackContext, | ||
}; | ||
use std::ffi::{c_char, CStr, CString}; | ||
|
||
unsafe extern "C" fn callback(ctx: FFICallbackContext, ptr: *const c_char) { | ||
let str = CStr::from_ptr(ptr).to_str().unwrap(); | ||
let ctx = &mut *(ctx as *mut usize); | ||
*ctx += 1; | ||
|
||
if *ctx % 2 == 0 { | ||
print!("{str}"); | ||
} else { | ||
eprint!("{str}"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More panic! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bro, panic abort my example :( |
||
} | ||
|
||
fn main() { | ||
let ctx = &mut 0usize as *mut usize; | ||
let level = CString::new("trace").unwrap(); | ||
unsafe { | ||
let handle = doublets_create_log_handle(ctx.cast(), callback, level.as_ptr(), true, false); | ||
|
||
tracing::error!("SOMETHING IS SERIOUSLY WRONG!!!"); | ||
tracing::warn!("important informational messages; might indicate an error"); | ||
tracing::info!("general informational messages relevant to users"); | ||
tracing::debug!("diagnostics used for internal debugging of a library or application"); | ||
tracing::trace!("very verbose diagnostic events"); | ||
Comment on lines
+54
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move logs out unsafe context. It's confusing |
||
|
||
doublets_free_log_handle(handle); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.