diff options
| author | chenzizhan <[email protected]> | 2023-10-08 14:32:12 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2023-10-08 14:32:12 +0800 |
| commit | 2161dc099e73aeeb7627a64728a1af573791d442 (patch) | |
| tree | e9d22346a5a7ffded853fe5907acbaeb25a704cc /src/c_lang | |
| parent | f9d79168f2f992ba1a825716ce8cd98290d96101 (diff) | |
export with fs handleczz_pub
Diffstat (limited to 'src/c_lang')
| -rw-r--r-- | src/c_lang/fieldstat.rs | 27 | ||||
| -rw-r--r-- | src/c_lang/fieldstat_binding.rs | 4 |
2 files changed, 14 insertions, 17 deletions
diff --git a/src/c_lang/fieldstat.rs b/src/c_lang/fieldstat.rs index aa80140..9838da6 100644 --- a/src/c_lang/fieldstat.rs +++ b/src/c_lang/fieldstat.rs @@ -859,12 +859,6 @@ impl Drop for Fieldstat { /// Output the fieldstat instance to json string array. /// -/// # Safety -/// This structure is unsafe when the lifetime of the instance is shorter than the lifetime of the JsonExporter. -/// Lifetime is not checked because we have to ensure the instance can be mutably borrowed after calling export. -/// -/// In multi-threaded environment, the instance binding to Exporter must be Read Only. -/// /// # Example /// ``` /// use fieldstat::*; @@ -872,7 +866,7 @@ impl Drop for Fieldstat { /// exporter.set_global_tag(global_tag.as_slice()); /// exporter.set_name("test abcde"); /// let ts = TimeVal::new(1, 2000); -/// let ret = exporter.export(ts).unwrap(); +/// let ret = exporter.export(&fs, ts).unwrap(); /// ``` /// ret is: /// ``` @@ -897,9 +891,9 @@ pub struct JsonExporter { } impl JsonExporter { - pub fn new(fs: &Fieldstat) -> Self { + pub fn new() -> Self { Self { - content: unsafe { fieldstat_binding::fieldstat_json_exporter_new(fs.content)}, + content: unsafe { fieldstat_binding::fieldstat_json_exporter_new()}, } } pub fn set_global_tag(&mut self, tags: &[FieldstatTagWrapper]) { @@ -917,10 +911,11 @@ impl JsonExporter { fieldstat_binding::fieldstat_json_exporter_set_name(self.content, c_name.as_ptr()) } } - pub fn export(&self, ts:TimeVal) -> Option<String> { + pub fn export(&self, fs:&Fieldstat, ts:TimeVal) -> Option<String> { unsafe { let ret = fieldstat_binding::fieldstat_json_exporter_export( self.content, + fs.content, &ts.as_c() as *const _, ); if ret == std::ptr::null_mut() { @@ -932,13 +927,14 @@ impl JsonExporter { Some(ret_str) } } - pub fn export_array(&self, ts:TimeVal) -> Vec<String> { + pub fn export_array(&self, fs:&Fieldstat, ts:TimeVal) -> Vec<String> { unsafe { let mut output: *mut *mut ::std::os::raw::c_char = std::ptr::null_mut(); let mut output_size: usize = 0; fieldstat_binding::fieldstat_json_exporter_export_array( self.content, + fs.content, &ts.as_c() as *const _, &mut output, &mut output_size, @@ -960,7 +956,6 @@ impl JsonExporter { /// let json exporter output delta value by the side of the original value(accumulated). If a cell / metric is new, the delta value is the same as the original value. /// Outputting delta value is disabled by default. /// When the exporter name or exporter global tags are changed, or the fieldstat instance is reset, or one of the cube is deleted, the delta value will be reset. (next output will be the original value) - /// it is recommended to call this function after fieldstat_json_exporter_set_name and fieldstat_json_exporter_set_global_tag. /// Only affects the metrics of counter type. /// Outputting delta value is time-consuming. pub fn enable_delta(&mut self) { @@ -1216,8 +1211,8 @@ mod tests { #[test] fn export() { // add some data - let mut fs = Fieldstat::new(); - let mut exporter = JsonExporter::new(&fs); + let mut fs: Fieldstat = Fieldstat::new(); + let mut exporter = JsonExporter::new(); let shared_tags = vec![ FieldstatTagWrapper::String(FieldstatTag::new("tag2", "abc".to_string())), ]; @@ -1235,7 +1230,7 @@ mod tests { exporter.set_global_tag(global_tag.as_slice()); exporter.set_name("test abcde"); let ts = TimeVal::new(1, 2000); - let ret = exporter.export(ts).unwrap(); + let ret = exporter.export(&fs, ts).unwrap(); // [{ // "name": "test abcde", @@ -1251,7 +1246,7 @@ mod tests { // }] let benchmark = "[{\"name\":\"test abcde\",\"tags\":{\"tag cell\":123,\"tag2\":\"abc\",\"global tag\":1},\"fields\":{\"test\":10},\"timestamp_ms\":1002}]"; assert_eq!(ret, benchmark); - let ret = exporter.export_array(ts); + let ret = exporter.export_array(&fs, ts); assert_eq!(ret.len(), 1); assert_eq!(ret[0], benchmark[1..benchmark.len()-1]); // remove '[]' } diff --git a/src/c_lang/fieldstat_binding.rs b/src/c_lang/fieldstat_binding.rs index b0fde7e..877feb2 100644 --- a/src/c_lang/fieldstat_binding.rs +++ b/src/c_lang/fieldstat_binding.rs @@ -415,7 +415,7 @@ pub struct fieldstat_json_exporter { _unused: [u8; 0], } extern "C" { - pub fn fieldstat_json_exporter_new(instance: *const fieldstat) -> *mut fieldstat_json_exporter; + pub fn fieldstat_json_exporter_new() -> *mut fieldstat_json_exporter; } extern "C" { pub fn fieldstat_json_exporter_set_global_tag( @@ -436,12 +436,14 @@ extern "C" { extern "C" { pub fn fieldstat_json_exporter_export( exporter: *const fieldstat_json_exporter, + instance: *const fieldstat, timestamp: *const TimeVal, ) -> *mut ::std::os::raw::c_char; } extern "C" { pub fn fieldstat_json_exporter_export_array( exporter: *const fieldstat_json_exporter, + instance: *const fieldstat, timestamp: *const TimeVal, output: *mut *mut *mut ::std::os::raw::c_char, output_size: *mut usize, |
