From d3fadc7c3cdb14a624c716c7395e7fb83b05dd72 Mon Sep 17 00:00:00 2001 From: Tymoteusz Jankowski Date: Fri, 26 Jan 2018 17:24:44 +0100 Subject: [PATCH] Add SelectView.get_item_mut --- src/views/select_view.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/views/select_view.rs b/src/views/select_view.rs index 9df0556..1078fd3 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -228,6 +228,21 @@ impl SelectView { self.items.get(i).map(|item| (item.label.as_ref(), &*item.value)) } + /// Gets a mut item at given idx or None. + pub fn get_item_mut(&mut self, i: usize) -> Option<(&mut String, &mut T)> { + if i >= self.items.len() { + None + } else { + let item = &mut self.items[i]; + if let Some(t) = Rc::get_mut(&mut item.value) { + let label = &mut item.label; + Some((label, t)) + } else { + None + } + } + } + /// Removes an item from the list. pub fn remove_item(&mut self, id: usize) { self.items.remove(id);