#Help me with this closure on egui

11 messages · Page 1 of 1 (latest)

restive pilot
#

you are trying to modify the services vec while you are iterating through it

#

which is simply Not Okay, since modifying the vec could invalidate the iterator - what if you're pushing into the vector and that reallocated the vector? the iterator would now point to freed memory, which is a big no no

#

the best you can do is to mark the service as removed, and actually remove it in the next frame/after the loop

actually, you can use Vec::retain_mut

#

something like this:

ui.vertical_centered(|ui| {
    ui.heading("Services");

    self.service_list.services.retain_mut(|service| {
        ui.horizontal(|ui| {
            ui.add(egui::TextEdit::multiline(&mut service.description));
            ui.add(egui::Slider::new(&mut service.amount_to_pay, 0.0..=1000.0).text("Amount to pay"));
            ui.add(egui::Slider::new(&mut service.quantity, 0.0..=100.0).text("Quantity"));

            !ui.button("Delete").on_hover_text("Delete this service").clicked()
        }).inner
    })
});
#

i'm not exactly sure if self.service_count is necessary, to be 100% honest

#

sounds like you can just do self.service_list.services.len()

restive pilot
#

we get the bool out via .inner and return it to retain_mut

#

in the end, the logic boils down to this: if the button is clicked, the entire closure returns false and the service is removed

#

if it's not, it is retained

#

or, exactly what you want ferrisBut but without double mutable borrows