From 2688a397aa7b27c1b03ff96ef4cf795f94f0a1e5 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 28 Jul 2019 15:27:50 +0300 Subject: [PATCH 1/5] Implement From> for dc_array_t --- src/dc_array.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/dc_array.rs b/src/dc_array.rs index b2834233a..b454fa716 100644 --- a/src/dc_array.rs +++ b/src/dc_array.rs @@ -138,6 +138,12 @@ impl dc_array_t { } } +impl From> for dc_array_t { + fn from(array: Vec) -> Self { + dc_array_t::Locations(array) + } +} + pub unsafe fn dc_array_unref(array: *mut dc_array_t) { if array.is_null() { return; From 14e42b48bdcd61588252ec1563a50d56b6aea70b Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 28 Jul 2019 15:29:47 +0300 Subject: [PATCH 2/5] dc_get_locations: use from(Vec) instead of add_location --- src/dc_location.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dc_location.rs b/src/dc_location.rs index bdef8671e..132d0f505 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -250,12 +250,12 @@ pub fn dc_get_locations( Ok(loc) }, |locations| { - let mut ret = dc_array_t::new_locations(500); + let mut ret = Vec::new(); for location in locations { - ret.add_location(location?); + ret.push(location?); } - Ok(ret.into_raw()) + Ok(dc_array_t::from(ret).into_raw()) }, ) .unwrap_or_else(|_| std::ptr::null_mut()) From 81a84620ebc784bb94580a83bf3d84a8a66e4c28 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 28 Jul 2019 15:50:41 +0300 Subject: [PATCH 3/5] Store dc_kml_t::locations as Option instead of pointer --- src/dc_location.rs | 39 ++++++++++++++++++--------------------- src/dc_mimeparser.rs | 4 ++-- src/dc_receive_imf.rs | 4 ++-- tests/stress.rs | 32 ++++++++++++++++---------------- 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/src/dc_location.rs b/src/dc_location.rs index 132d0f505..5e78f20f7 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -51,7 +51,7 @@ impl dc_location { #[allow(non_camel_case_types)] pub struct dc_kml_t { pub addr: *mut libc::c_char, - pub locations: *mut dc_array_t, + pub locations: Option, pub tag: libc::c_int, pub curr: dc_location, } @@ -60,7 +60,7 @@ impl dc_kml_t { pub fn new() -> Self { dc_kml_t { addr: std::ptr::null_mut(), - locations: std::ptr::null_mut(), + locations: None, tag: 0, curr: dc_location::new(), } @@ -422,13 +422,14 @@ pub unsafe fn dc_save_locations( context: &Context, chat_id: u32, contact_id: u32, - locations: *const dc_array_t, + locations_opt: &Option, independent: libc::c_int, ) -> u32 { - if chat_id <= 9 || locations.is_null() { + if chat_id <= 9 || locations_opt.is_none() { return 0; } + let locations = locations_opt.as_ref().unwrap(); context .sql .prepare2( @@ -440,31 +441,31 @@ pub unsafe fn dc_save_locations( let mut newest_timestamp = 0; let mut newest_location_id = 0; - for i in 0..dc_array_get_cnt(locations) { - let location = dc_array_get_ptr(locations, i as size_t) as *mut dc_location; + for i in 0..locations.len() { + let location = locations.get_location(i as size_t); let exists = - stmt_test.exists(params![(*location).timestamp, contact_id as i32])?; + stmt_test.exists(params![location.timestamp, contact_id as i32])?; if 0 != independent || !exists { stmt_insert.execute(params![ - (*location).timestamp, + location.timestamp, contact_id as i32, chat_id as i32, - (*location).latitude, - (*location).longitude, - (*location).accuracy, + location.latitude, + location.longitude, + location.accuracy, independent, ])?; - if (*location).timestamp > newest_timestamp { - newest_timestamp = (*location).timestamp; + if location.timestamp > newest_timestamp { + newest_timestamp = location.timestamp; newest_location_id = sql::get_rowid2_with_conn( context, conn, "locations", "timestamp", - (*location).timestamp, + location.timestamp, "from_id", contact_id as i32, ); @@ -499,7 +500,7 @@ pub unsafe fn dc_kml_parse( } else { content_nullterminated = dc_null_terminate(content, content_bytes as libc::c_int); if !content_nullterminated.is_null() { - kml.locations = dc_array_new_locations(100); + kml.locations = Some(dc_array_t::new_locations(100)); dc_saxparser_init( &mut saxparser, &mut kml as *mut dc_kml_t as *mut libc::c_void, @@ -585,7 +586,7 @@ unsafe fn kml_endtag_cb(userdata: *mut libc::c_void, tag: *const libc::c_char) { && 0. != (*kml).curr.longitude { let location = (*kml).curr.clone(); - (*(*kml).locations).add_location(location); + ((*kml).locations.as_mut().unwrap()).add_location(location); } (*kml).tag = 0 }; @@ -636,11 +637,7 @@ unsafe fn kml_starttag_cb( }; } -pub unsafe fn dc_kml_unref(kml: *mut dc_kml_t) { - if kml.is_null() { - return; - } - dc_array_unref((*kml).locations); +pub unsafe fn dc_kml_unref(kml: &mut dc_kml_t) { free((*kml).addr as *mut libc::c_void); } diff --git a/src/dc_mimeparser.rs b/src/dc_mimeparser.rs index f5bc4cc64..c190d480a 100644 --- a/src/dc_mimeparser.rs +++ b/src/dc_mimeparser.rs @@ -135,12 +135,12 @@ pub unsafe fn dc_mimeparser_empty(mimeparser: &mut dc_mimeparser_t) { dc_e2ee_thanks(&mut (*mimeparser).e2ee_helper); if let Some(location_kml) = (*mimeparser).location_kml.as_mut() { - dc_kml_unref(location_kml as *mut dc_kml_t); + dc_kml_unref(location_kml); } (*mimeparser).location_kml = None; if let Some(message_kml) = (*mimeparser).message_kml.as_mut() { - dc_kml_unref(message_kml as *mut dc_kml_t); + dc_kml_unref(message_kml); } (*mimeparser).message_kml = None; } diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 35964de91..4dd2d424c 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -771,7 +771,7 @@ pub unsafe fn dc_receive_imf( context, chat_id, from_id, - mime_parser.message_kml.unwrap().locations, + &mime_parser.message_kml.unwrap().locations, 1, ); if 0 != newest_location_id && 0 == hidden { @@ -797,7 +797,7 @@ pub unsafe fn dc_receive_imf( context, chat_id, from_id, - mime_parser.location_kml.as_ref().unwrap().locations, + &mime_parser.location_kml.as_ref().unwrap().locations, 0, ); if newest_location_id != 0 && hidden == 0 && !location_id_written { diff --git a/tests/stress.rs b/tests/stress.rs index 1a1e19d87..141d8c9fc 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -840,24 +840,24 @@ fn test_dc_kml_parse() { assert!(!kml.addr.is_null()); assert_eq!(as_str(kml.addr as *const libc::c_char), "user@example.org",); - assert_eq!(dc_array_get_cnt(kml.locations), 2); + let locations_ref = kml.locations.as_ref().unwrap(); + assert_eq!(locations_ref.len(), 2); - assert!(dc_array_get_latitude(kml.locations, 0) > 53.6f64); - assert!(dc_array_get_latitude(kml.locations, 0) < 53.8f64); - assert!(dc_array_get_longitude(kml.locations, 0) > 9.3f64); - assert!(dc_array_get_longitude(kml.locations, 0) < 9.5f64); - assert!(dc_array_get_accuracy(kml.locations, 0) > 31.9f64); - assert!(dc_array_get_accuracy(kml.locations, 0) < 32.1f64); - assert_eq!(dc_array_get_timestamp(kml.locations, 0), 1551906597); + assert!(locations_ref.get_latitude(0) > 53.6f64); + assert!(locations_ref.get_latitude(0) < 53.8f64); + assert!(locations_ref.get_longitude(0) > 9.3f64); + assert!(locations_ref.get_longitude(0) < 9.5f64); + assert!(locations_ref.get_accuracy(0) > 31.9f64); + assert!(locations_ref.get_accuracy(0) < 32.1f64); + assert_eq!(locations_ref.get_timestamp(0), 1551906597); - assert!(dc_array_get_latitude(kml.locations, 1) > 63.6f64); - assert!(dc_array_get_latitude(kml.locations, 1) < 63.8f64); - assert!(dc_array_get_longitude(kml.locations, 1) > 19.3f64); - assert!(dc_array_get_longitude(kml.locations, 1) < 19.5f64); - assert!(dc_array_get_accuracy(kml.locations, 1) > 2.4f64); - assert!(dc_array_get_accuracy(kml.locations, 1) < 2.6f64); - - assert_eq!(dc_array_get_timestamp(kml.locations, 1), 1544739072); + assert!(locations_ref.get_latitude(1) > 63.6f64); + assert!(locations_ref.get_latitude(1) < 63.8f64); + assert!(locations_ref.get_longitude(1) > 19.3f64); + assert!(locations_ref.get_longitude(1) < 19.5f64); + assert!(locations_ref.get_accuracy(1) > 2.4f64); + assert!(locations_ref.get_accuracy(1) < 2.6f64); + assert_eq!(locations_ref.get_timestamp(1), 1544739072); dc_kml_unref(&mut kml); } From ae6c41a01987e2af7ff396f49a4553489c166c17 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 28 Jul 2019 16:07:04 +0300 Subject: [PATCH 4/5] dc_kml_t: replace Option with Option> --- src/dc_location.rs | 10 +++++----- tests/stress.rs | 30 +++++++++++++++--------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/dc_location.rs b/src/dc_location.rs index 5e78f20f7..5977a939b 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -51,7 +51,7 @@ impl dc_location { #[allow(non_camel_case_types)] pub struct dc_kml_t { pub addr: *mut libc::c_char, - pub locations: Option, + pub locations: Option>, pub tag: libc::c_int, pub curr: dc_location, } @@ -422,7 +422,7 @@ pub unsafe fn dc_save_locations( context: &Context, chat_id: u32, contact_id: u32, - locations_opt: &Option, + locations_opt: &Option>, independent: libc::c_int, ) -> u32 { if chat_id <= 9 || locations_opt.is_none() { @@ -442,7 +442,7 @@ pub unsafe fn dc_save_locations( let mut newest_location_id = 0; for i in 0..locations.len() { - let location = locations.get_location(i as size_t); + let location = &locations[i]; let exists = stmt_test.exists(params![location.timestamp, contact_id as i32])?; @@ -500,7 +500,7 @@ pub unsafe fn dc_kml_parse( } else { content_nullterminated = dc_null_terminate(content, content_bytes as libc::c_int); if !content_nullterminated.is_null() { - kml.locations = Some(dc_array_t::new_locations(100)); + kml.locations = Some(Vec::with_capacity(100)); dc_saxparser_init( &mut saxparser, &mut kml as *mut dc_kml_t as *mut libc::c_void, @@ -586,7 +586,7 @@ unsafe fn kml_endtag_cb(userdata: *mut libc::c_void, tag: *const libc::c_char) { && 0. != (*kml).curr.longitude { let location = (*kml).curr.clone(); - ((*kml).locations.as_mut().unwrap()).add_location(location); + ((*kml).locations.as_mut().unwrap()).push(location); } (*kml).tag = 0 }; diff --git a/tests/stress.rs b/tests/stress.rs index 141d8c9fc..164a937bb 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -840,24 +840,24 @@ fn test_dc_kml_parse() { assert!(!kml.addr.is_null()); assert_eq!(as_str(kml.addr as *const libc::c_char), "user@example.org",); - let locations_ref = kml.locations.as_ref().unwrap(); + let locations_ref = &kml.locations.as_ref().unwrap(); assert_eq!(locations_ref.len(), 2); - assert!(locations_ref.get_latitude(0) > 53.6f64); - assert!(locations_ref.get_latitude(0) < 53.8f64); - assert!(locations_ref.get_longitude(0) > 9.3f64); - assert!(locations_ref.get_longitude(0) < 9.5f64); - assert!(locations_ref.get_accuracy(0) > 31.9f64); - assert!(locations_ref.get_accuracy(0) < 32.1f64); - assert_eq!(locations_ref.get_timestamp(0), 1551906597); + assert!(locations_ref[0].latitude > 53.6f64); + assert!(locations_ref[0].latitude < 53.8f64); + assert!(locations_ref[0].longitude > 9.3f64); + assert!(locations_ref[0].longitude < 9.5f64); + assert!(locations_ref[0].accuracy > 31.9f64); + assert!(locations_ref[0].accuracy < 32.1f64); + assert_eq!(locations_ref[0].timestamp, 1551906597); - assert!(locations_ref.get_latitude(1) > 63.6f64); - assert!(locations_ref.get_latitude(1) < 63.8f64); - assert!(locations_ref.get_longitude(1) > 19.3f64); - assert!(locations_ref.get_longitude(1) < 19.5f64); - assert!(locations_ref.get_accuracy(1) > 2.4f64); - assert!(locations_ref.get_accuracy(1) < 2.6f64); - assert_eq!(locations_ref.get_timestamp(1), 1544739072); + assert!(locations_ref[1].latitude > 63.6f64); + assert!(locations_ref[1].latitude < 63.8f64); + assert!(locations_ref[1].longitude > 19.3f64); + assert!(locations_ref[1].longitude < 19.5f64); + assert!(locations_ref[1].accuracy > 2.4f64); + assert!(locations_ref[1].accuracy < 2.6f64); + assert_eq!(locations_ref[1].timestamp, 1544739072); dc_kml_unref(&mut kml); } From 76e76470e0f1d313374d6a1f3144636a8331d891 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sun, 28 Jul 2019 21:36:17 +0300 Subject: [PATCH 5/5] Replace range loop with foreach loop --- src/dc_location.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/dc_location.rs b/src/dc_location.rs index 5977a939b..b16e3cb74 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -441,9 +441,7 @@ pub unsafe fn dc_save_locations( let mut newest_timestamp = 0; let mut newest_location_id = 0; - for i in 0..locations.len() { - let location = &locations[i]; - + for location in locations { let exists = stmt_test.exists(params![location.timestamp, contact_id as i32])?;