From 09833eb74dccfce18009d9e41d77d3b0a9c32ae2 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Wed, 24 Jun 2020 20:50:51 +0300 Subject: [PATCH] dc_array: introduce MsgIds variant This avoids allocation of u32 vector. --- deltachat-ffi/src/dc_array.rs | 10 ++++++++++ deltachat-ffi/src/lib.rs | 18 ++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/deltachat-ffi/src/dc_array.rs b/deltachat-ffi/src/dc_array.rs index 48c0d4985..6d14186b0 100644 --- a/deltachat-ffi/src/dc_array.rs +++ b/deltachat-ffi/src/dc_array.rs @@ -1,8 +1,10 @@ use crate::location::Location; +use crate::message::MsgId; /* * the structure behind dc_array_t */ #[derive(Debug, Clone)] pub enum dc_array_t { + MsgIds(Vec), Locations(Vec), Uint(Vec), } @@ -10,6 +12,7 @@ pub enum dc_array_t { impl dc_array_t { pub(crate) fn get_id(&self, index: usize) -> u32 { match self { + Self::MsgIds(array) => array[index].to_u32(), Self::Locations(array) => array[index].location_id, Self::Uint(array) => array[index], } @@ -26,6 +29,7 @@ impl dc_array_t { /// Returns the number of elements in the array. pub(crate) fn len(&self) -> usize { match self { + Self::MsgIds(array) => array.len(), Self::Locations(array) => array.len(), Self::Uint(array) => array.len(), } @@ -50,6 +54,12 @@ impl From> for dc_array_t { } } +impl From> for dc_array_t { + fn from(array: Vec) -> Self { + dc_array_t::MsgIds(array) + } +} + impl From> for dc_array_t { fn from(array: Vec) -> Self { dc_array_t::Locations(array) diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 261461ce2..ee04e72a6 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -830,14 +830,11 @@ pub unsafe extern "C" fn dc_get_chat_msgs( }; block_on(async move { - let arr = dc_array_t::from( + Box::into_raw(Box::new( chat::get_chat_msgs(&ctx, ChatId::new(chat_id), flags, marker_flag) .await - .iter() - .map(|msg_id| msg_id.to_u32()) - .collect::>(), - ); - Box::into_raw(Box::new(arr)) + .into(), + )) }) } @@ -966,7 +963,7 @@ pub unsafe extern "C" fn dc_get_chat_media( from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3)); block_on(async move { - let arr = dc_array_t::from( + Box::into_raw(Box::new( chat::get_chat_media( &ctx, ChatId::new(chat_id), @@ -975,11 +972,8 @@ pub unsafe extern "C" fn dc_get_chat_media( or_msg_type3, ) .await - .iter() - .map(|msg_id| msg_id.to_u32()) - .collect::>(), - ); - Box::into_raw(Box::new(arr)) + .into(), + )) }) }