diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 2d3775eb7..c919d660d 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -428,7 +428,8 @@ pub unsafe extern "C" fn dc_get_chat_msgs( assert!(!context.is_null()); let context = &*context; - dc_array_t::from(chat::get_chat_msgs(context, chat_id, flags, marker1before)).into_raw() + let arr = dc_array_t::from(chat::get_chat_msgs(context, chat_id, flags, marker1before)); + Box::into_raw(Box::new(arr)) } #[no_mangle] @@ -457,7 +458,8 @@ pub unsafe extern "C" fn dc_get_fresh_msgs( assert!(!context.is_null()); let context = &*context; - dc_array_t::from(context::dc_get_fresh_msgs(context)).into_raw() + let arr = dc_array_t::from(context::dc_get_fresh_msgs(context)); + Box::into_raw(Box::new(arr)) } #[no_mangle] @@ -501,14 +503,14 @@ pub unsafe extern "C" fn dc_get_chat_media( let or_msg_type3 = from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3)); - dc_array_t::from(chat::get_chat_media( + let arr = dc_array_t::from(chat::get_chat_media( context, chat_id, msg_type, or_msg_type2, or_msg_type3, - )) - .into_raw() + )); + Box::into_raw(Box::new(arr)) } #[no_mangle] @@ -568,7 +570,8 @@ pub unsafe extern "C" fn dc_get_chat_contacts( assert!(!context.is_null()); let context = &*context; - dc_array_t::from(chat::get_chat_contacts(context, chat_id)).into_raw() + let arr = dc_array_t::from(chat::get_chat_contacts(context, chat_id)); + Box::into_raw(Box::new(arr)) } #[no_mangle] @@ -581,7 +584,8 @@ pub unsafe extern "C" fn dc_search_msgs( assert!(!query.is_null()); let context = &*context; - dc_array_t::from(context::dc_search_msgs(context, chat_id, query)).into_raw() + let arr = dc_array_t::from(context::dc_search_msgs(context, chat_id, query)); + Box::into_raw(Box::new(arr)) } #[no_mangle] @@ -856,7 +860,7 @@ pub unsafe extern "C" fn dc_get_contacts( }; match Contact::get_all(context, flags, query) { - Ok(contacts) => dc_array_t::from(contacts).into_raw(), + Ok(contacts) => Box::into_raw(Box::new(dc_array_t::from(contacts))), Err(_) => std::ptr::null_mut(), } } @@ -876,7 +880,9 @@ pub unsafe extern "C" fn dc_get_blocked_contacts( assert!(!context.is_null()); let context = &*context; - dc_array_t::from(Contact::get_all_blocked(context)).into_raw() + Box::into_raw(Box::new(dc_array_t::from(Contact::get_all_blocked( + context, + )))) } #[no_mangle] @@ -1082,7 +1088,7 @@ pub unsafe extern "C" fn dc_get_locations( timestamp_begin as i64, timestamp_end as i64, ); - dc_array_t::from(res).into_raw() + Box::into_raw(Box::new(dc_array_t::from(res))) } #[no_mangle] @@ -1102,30 +1108,27 @@ pub type dc_array_t = dc_array::dc_array_t; pub unsafe extern "C" fn dc_array_unref(a: *mut dc_array::dc_array_t) { assert!(!a.is_null()); - dc_array::dc_array_unref(a) + Box::from_raw(a); } #[no_mangle] pub unsafe extern "C" fn dc_array_add_id(array: *mut dc_array_t, item: libc::c_uint) { assert!(!array.is_null()); - dc_array::dc_array_add_id(array, item) + (*array).add_id(item); } #[no_mangle] pub unsafe extern "C" fn dc_array_get_cnt(array: *const dc_array_t) -> libc::size_t { assert!(!array.is_null()); - dc_array::dc_array_get_cnt(array) + (*array).len() } #[no_mangle] -pub unsafe extern "C" fn dc_array_get_id( - array: *const dc_array_t, - index: libc::size_t, -) -> libc::c_uint { +pub unsafe extern "C" fn dc_array_get_id(array: *const dc_array_t, index: libc::size_t) -> u32 { assert!(!array.is_null()); - dc_array::dc_array_get_id(array, index) + (*array).get_id(index) } #[no_mangle] pub unsafe extern "C" fn dc_array_get_latitude( @@ -1134,7 +1137,7 @@ pub unsafe extern "C" fn dc_array_get_latitude( ) -> libc::c_double { assert!(!array.is_null()); - (*array).get_latitude(index) + (*array).get_location(index).latitude } #[no_mangle] pub unsafe extern "C" fn dc_array_get_longitude( @@ -1143,7 +1146,7 @@ pub unsafe extern "C" fn dc_array_get_longitude( ) -> libc::c_double { assert!(!array.is_null()); - (*array).get_longitude(index) + (*array).get_location(index).longitude } #[no_mangle] pub unsafe extern "C" fn dc_array_get_accuracy( @@ -1152,7 +1155,7 @@ pub unsafe extern "C" fn dc_array_get_accuracy( ) -> libc::c_double { assert!(!array.is_null()); - (*array).get_accuracy(index) + (*array).get_location(index).accuracy } #[no_mangle] pub unsafe extern "C" fn dc_array_get_timestamp( @@ -1161,7 +1164,7 @@ pub unsafe extern "C" fn dc_array_get_timestamp( ) -> i64 { assert!(!array.is_null()); - (*array).get_timestamp(index) + (*array).get_location(index).timestamp } #[no_mangle] pub unsafe extern "C" fn dc_array_get_chat_id( @@ -1170,7 +1173,7 @@ pub unsafe extern "C" fn dc_array_get_chat_id( ) -> libc::c_uint { assert!(!array.is_null()); - (*array).get_chat_id(index) + (*array).get_location(index).chat_id } #[no_mangle] pub unsafe extern "C" fn dc_array_get_contact_id( @@ -1179,7 +1182,7 @@ pub unsafe extern "C" fn dc_array_get_contact_id( ) -> libc::c_uint { assert!(!array.is_null()); - (*array).get_contact_id(index) + (*array).get_location(index).contact_id } #[no_mangle] pub unsafe extern "C" fn dc_array_get_msg_id( @@ -1188,7 +1191,7 @@ pub unsafe extern "C" fn dc_array_get_msg_id( ) -> libc::c_uint { assert!(!array.is_null()); - (*array).get_msg_id(index) + (*array).get_location(index).msg_id } #[no_mangle] pub unsafe extern "C" fn dc_array_get_marker( @@ -1197,7 +1200,11 @@ pub unsafe extern "C" fn dc_array_get_marker( ) -> *mut libc::c_char { assert!(!array.is_null()); - dc_array::dc_array_get_marker(array, index) + if let Some(s) = &(*array).get_location(index).marker { + s.strdup() + } else { + std::ptr::null_mut() + } } #[no_mangle] @@ -1208,16 +1215,27 @@ pub unsafe extern "C" fn dc_array_search_id( ) -> libc::c_int { assert!(!array.is_null()); - dc_array::dc_array_search_id(array, needle, ret_index) as libc::c_int + if let Some(i) = (*array).search_id(needle) { + if !ret_index.is_null() { + *ret_index = i + } + 1 + } else { + 0 + } } #[no_mangle] pub unsafe extern "C" fn dc_array_get_raw(array: *const dc_array_t) -> *const u32 { assert!(!array.is_null()); - dc_array::dc_array_get_raw(array) + (*array).as_ptr() } +// Return the independent-state of the location at the given index. +// Independent locations do not belong to the track of the user. +// Returns 1 if location belongs to the track of the user, +// 0 if location was reported independently. #[no_mangle] pub unsafe fn dc_array_is_independent( array: *const dc_array_t, @@ -1225,7 +1243,7 @@ pub unsafe fn dc_array_is_independent( ) -> libc::c_int { assert!(!array.is_null()); - dc_array::dc_array_is_independent(array, index) + (*array).get_location(index).independent as libc::c_int } // dc_chatlist_t diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index f646e7940..a95347f71 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -176,7 +176,7 @@ class Account(object): whose name or e-mail matches query. :param only_verified: if true only return verified contacts. :param with_self: if true the self-contact is also returned. - :returns: list of :class:`deltachat.message.Message` objects. + :returns: list of :class:`deltachat.chatting.Contact` objects. """ flags = 0 query = as_dc_charpointer(query) diff --git a/src/dc_array.rs b/src/dc_array.rs index a561dd8a2..a764b6cf6 100644 --- a/src/dc_array.rs +++ b/src/dc_array.rs @@ -1,5 +1,4 @@ use crate::dc_location::dc_location; -use crate::dc_tools::*; use crate::types::*; /* * the structure behind dc_array_t */ @@ -20,10 +19,6 @@ impl dc_array_t { dc_array_t::Locations(Vec::with_capacity(capacity)) } - pub fn into_raw(self) -> *mut Self { - Box::into_raw(Box::new(self)) - } - pub fn add_id(&mut self, item: uint32_t) { if let Self::Uint(array) = self { array.push(item); @@ -55,34 +50,6 @@ impl dc_array_t { } } - pub fn get_latitude(&self, index: usize) -> libc::c_double { - self.get_location(index).latitude - } - - pub fn get_longitude(&self, index: size_t) -> libc::c_double { - self.get_location(index).longitude - } - - pub fn get_accuracy(&self, index: size_t) -> libc::c_double { - self.get_location(index).accuracy - } - - pub fn get_timestamp(&self, index: size_t) -> i64 { - self.get_location(index).timestamp - } - - pub fn get_chat_id(&self, index: size_t) -> uint32_t { - self.get_location(index).chat_id - } - - pub fn get_contact_id(&self, index: size_t) -> uint32_t { - self.get_location(index).contact_id - } - - pub fn get_msg_id(&self, index: size_t) -> uint32_t { - self.get_location(index).msg_id - } - pub fn is_empty(&self) -> bool { match self { Self::Locations(array) => array.is_empty(), @@ -125,6 +92,14 @@ impl dc_array_t { panic!("Attempt to sort array of something other than uints"); } } + + pub fn as_ptr(&self) -> *const u32 { + if let dc_array_t::Uint(v) = self { + v.as_ptr() + } else { + panic!("Attempt to convert array of something other than uints to raw"); + } + } } impl From> for dc_array_t { @@ -139,155 +114,50 @@ impl From> for dc_array_t { } } -pub unsafe fn dc_array_unref(array: *mut dc_array_t) { - assert!(!array.is_null()); - Box::from_raw(array); -} - -pub unsafe fn dc_array_add_id(array: *mut dc_array_t, item: uint32_t) { - assert!(!array.is_null()); - (*array).add_id(item); -} - -pub unsafe fn dc_array_get_cnt(array: *const dc_array_t) -> size_t { - assert!(!array.is_null()); - (*array).len() -} - -pub unsafe fn dc_array_get_id(array: *const dc_array_t, index: size_t) -> uint32_t { - assert!(!array.is_null()); - (*array).get_id(index) -} - -pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *mut libc::c_char { - assert!(!array.is_null()); - - if let dc_array_t::Locations(v) = &*array { - if let Some(s) = &v[index].marker { - s.strdup() - } else { - std::ptr::null_mut() - } - } else { - panic!("Not an array of locations"); - } -} - -/** - * Return the independent-state of the location at the given index. - * Independent locations do not belong to the track of the user. - * - * @memberof dc_array_t - * @param array The array object. - * @param index Index of the item. Must be between 0 and dc_array_get_cnt()-1. - * @return 0=Location belongs to the track of the user, - * 1=Location was reported independently. - */ -pub unsafe fn dc_array_is_independent(array: *const dc_array_t, index: size_t) -> libc::c_int { - assert!(!array.is_null()); - - if let dc_array_t::Locations(v) = &*array { - v[index].independent as libc::c_int - } else { - panic!("Attempt to get location independent field from array of something other than locations"); - } -} - -pub unsafe fn dc_array_search_id( - array: *const dc_array_t, - needle: uint32_t, - ret_index: *mut size_t, -) -> bool { - assert!(!array.is_null()); - - if let Some(i) = (*array).search_id(needle) { - if !ret_index.is_null() { - *ret_index = i - } - true - } else { - false - } -} - -pub unsafe fn dc_array_get_raw(array: *const dc_array_t) -> *const u32 { - assert!(!array.is_null()); - - if let dc_array_t::Uint(v) = &*array { - v.as_ptr() - } else { - panic!("Attempt to convert array of something other than uints to raw"); - } -} - -pub fn dc_array_new(initsize: size_t) -> *mut dc_array_t { - dc_array_t::new(initsize).into_raw() -} - -#[cfg(test)] -unsafe fn dc_array_empty(array: *mut dc_array_t) { - assert!(!array.is_null()); - - (*array).clear() -} - -pub unsafe fn dc_array_duplicate(array: *const dc_array_t) -> *mut dc_array_t { - assert!(!array.is_null()); - - (*array).clone().into_raw() -} - #[cfg(test)] mod tests { use super::*; #[test] fn test_dc_array() { - unsafe { - let arr = dc_array_new(7 as size_t); - assert_eq!(dc_array_get_cnt(arr), 0); + let mut arr = dc_array_t::new(7); + assert!(arr.is_empty()); - for i in 0..1000 { - dc_array_add_id(arr, (i + 2) as uint32_t); - } - - assert_eq!(dc_array_get_cnt(arr), 1000); - - for i in 0..1000 { - assert_eq!( - dc_array_get_id(arr, i as size_t), - (i + 1i32 * 2i32) as libc::c_uint - ); - } - - dc_array_empty(arr); - - assert_eq!(dc_array_get_cnt(arr), 0); - - dc_array_add_id(arr, 13 as uint32_t); - dc_array_add_id(arr, 7 as uint32_t); - dc_array_add_id(arr, 666 as uint32_t); - dc_array_add_id(arr, 0 as uint32_t); - dc_array_add_id(arr, 5000 as uint32_t); - - (*arr).sort_ids(); - - assert_eq!(dc_array_get_id(arr, 0 as size_t), 0); - assert_eq!(dc_array_get_id(arr, 1 as size_t), 7); - assert_eq!(dc_array_get_id(arr, 2 as size_t), 13); - assert_eq!(dc_array_get_id(arr, 3 as size_t), 666); - - dc_array_unref(arr); + for i in 0..1000 { + arr.add_id(i + 2); } + + assert_eq!(arr.len(), 1000); + + for i in 0..1000 { + assert_eq!(arr.get_id(i), (i + 2) as u32); + } + + arr.clear(); + + assert!(arr.is_empty()); + + arr.add_id(13); + arr.add_id(7); + arr.add_id(666); + arr.add_id(0); + arr.add_id(5000); + + arr.sort_ids(); + + assert_eq!(arr.get_id(0), 0); + assert_eq!(arr.get_id(1), 7); + assert_eq!(arr.get_id(2), 13); + assert_eq!(arr.get_id(3), 666); } #[test] #[should_panic] fn test_dc_array_out_of_bounds() { - let arr = dc_array_new(7); + let mut arr = dc_array_t::new(7); for i in 0..1000 { - unsafe { dc_array_add_id(arr, (i + 2) as uint32_t) }; + arr.add_id(i + 2); } - unsafe { dc_array_get_id(arr, 1000) }; + arr.get_id(1000); } }