From 76b7e7408a643e5118c59ef1a15a2463ef072ebe Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sat, 28 Mar 2020 03:13:05 +0300 Subject: [PATCH] chatlist: remove indexing in get_{chat,msg}_id() --- src/chatlist.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/chatlist.rs b/src/chatlist.rs index cec5b520c..a681febfa 100644 --- a/src/chatlist.rs +++ b/src/chatlist.rs @@ -5,7 +5,7 @@ use crate::chat::*; use crate::constants::*; use crate::contact::*; use crate::context::*; -use crate::error::{ensure, Result}; +use crate::error::{bail, ensure, Result}; use crate::lot::Lot; use crate::message::{Message, MessageState, MsgId}; use crate::stock::StockMessage; @@ -275,18 +275,20 @@ impl Chatlist { /// /// To get the message object from the message ID, use dc_get_chat(). pub fn get_chat_id(&self, index: usize) -> ChatId { - if index >= self.ids.len() { - return ChatId::new(0); + match self.ids.get(index) { + Some((chat_id, _msg_id)) => *chat_id, + None => ChatId::new(0), } - self.ids[index].0 } /// Get a single message ID of a chatlist. /// /// To get the message object from the message ID, use dc_get_msg(). pub fn get_msg_id(&self, index: usize) -> Result { - ensure!(index < self.ids.len(), "Chatlist index out of range"); - Ok(self.ids[index].1) + match self.ids.get(index) { + Some((_chat_id, msg_id)) => Ok(*msg_id), + None => bail!("Chatlist index out of range"), + } } /// Get a summary for a chatlist index.