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::message::MsgId;
/* * the structure behind dc_array_t */
#[derive(Debug, Clone)]
pub enum dc_array_t {
MsgIds(Vec<MsgId>),
Locations(Vec<Location>),
Uint(Vec<u32>),
}
@@ -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<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 {
fn from(array: Vec<Location>) -> Self {
dc_array_t::Locations(array)