From ec10145bf25c644c214859b42a4ec472d97b911c Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 9 May 2020 18:55:37 -0700 Subject: [PATCH] Add SelectView::(try_)iter_mut --- cursive-core/src/views/select_view.rs | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cursive-core/src/views/select_view.rs b/cursive-core/src/views/select_view.rs index a545be0..59b589f 100644 --- a/cursive-core/src/views/select_view.rs +++ b/cursive-core/src/views/select_view.rs @@ -326,6 +326,39 @@ impl SelectView { } } + /// Iterate mutably on the items in this view. + /// + /// Returns an iterator with each item and their labels. + /// + /// In some cases some items will need to be cloned (for example if a + /// `Rc` is still alive after calling `SelectView::selection()`). + /// + /// If `T` does not implement `Clone`, check `SelectView::try_iter_mut()`. + pub fn iter_mut( + &mut self, + ) -> impl Iterator + where + T: Clone, + { + self.items + .iter_mut() + .map(|item| (&mut item.label, Rc::make_mut(&mut item.value))) + } + + /// Try to iterate mutably on the items in this view. + /// + /// Returns an iterator with each item and their labels. + /// + /// Some items may not be returned mutably, for example if a `Rc` is + /// still alive after calling `SelectView::selection()`. + pub fn try_iter_mut( + &mut self, + ) -> impl Iterator)> { + self.items + .iter_mut() + .map(|item| (&mut item.label, Rc::get_mut(&mut item.value))) + } + /// Iterate on the items in this view. /// /// Returns an iterator with each item and their labels.