dc_array: introduce MsgIds variant

This avoids allocation of u32 vector.
This commit is contained in:
Alexander Krotov
2020-06-24 20:50:51 +03:00
committed by link2xt
parent 2c11df46a7
commit 09833eb74d
2 changed files with 16 additions and 12 deletions

View File

@@ -1,8 +1,10 @@
use crate::location::Location; use crate::location::Location;
use crate::message::MsgId;
/* * the structure behind dc_array_t */ /* * the structure behind dc_array_t */
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum dc_array_t { pub enum dc_array_t {
MsgIds(Vec<MsgId>),
Locations(Vec<Location>), Locations(Vec<Location>),
Uint(Vec<u32>), Uint(Vec<u32>),
} }
@@ -10,6 +12,7 @@ pub enum dc_array_t {
impl dc_array_t { impl dc_array_t {
pub(crate) fn get_id(&self, index: usize) -> u32 { pub(crate) fn get_id(&self, index: usize) -> u32 {
match self { match self {
Self::MsgIds(array) => array[index].to_u32(),
Self::Locations(array) => array[index].location_id, Self::Locations(array) => array[index].location_id,
Self::Uint(array) => array[index], Self::Uint(array) => array[index],
} }
@@ -26,6 +29,7 @@ impl dc_array_t {
/// Returns the number of elements in the array. /// Returns the number of elements in the array.
pub(crate) fn len(&self) -> usize { pub(crate) fn len(&self) -> usize {
match self { match self {
Self::MsgIds(array) => array.len(),
Self::Locations(array) => array.len(), Self::Locations(array) => array.len(),
Self::Uint(array) => array.len(), Self::Uint(array) => array.len(),
} }
@@ -50,6 +54,12 @@ impl From<Vec<u32>> for dc_array_t {
} }
} }
impl From<Vec<MsgId>> for dc_array_t {
fn from(array: Vec<MsgId>) -> Self {
dc_array_t::MsgIds(array)
}
}
impl From<Vec<Location>> for dc_array_t { impl From<Vec<Location>> for dc_array_t {
fn from(array: Vec<Location>) -> Self { fn from(array: Vec<Location>) -> Self {
dc_array_t::Locations(array) dc_array_t::Locations(array)

View File

@@ -830,14 +830,11 @@ pub unsafe extern "C" fn dc_get_chat_msgs(
}; };
block_on(async move { 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) chat::get_chat_msgs(&ctx, ChatId::new(chat_id), flags, marker_flag)
.await .await
.iter() .into(),
.map(|msg_id| msg_id.to_u32()) ))
.collect::<Vec<u32>>(),
);
Box::into_raw(Box::new(arr))
}) })
} }
@@ -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)); from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3));
block_on(async move { block_on(async move {
let arr = dc_array_t::from( Box::into_raw(Box::new(
chat::get_chat_media( chat::get_chat_media(
&ctx, &ctx,
ChatId::new(chat_id), ChatId::new(chat_id),
@@ -975,11 +972,8 @@ pub unsafe extern "C" fn dc_get_chat_media(
or_msg_type3, or_msg_type3,
) )
.await .await
.iter() .into(),
.map(|msg_id| msg_id.to_u32()) ))
.collect::<Vec<u32>>(),
);
Box::into_raw(Box::new(arr))
}) })
} }