Skip to content

Commit

Permalink
Remove clone of value in the promise
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Feb 7, 2024
1 parent 93f4e46 commit 2a11498
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ impl eframe::App for BrowseApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// assign sample text once it comes in
if let Some(promise) = &self.promise {
if let Some(result) = promise.ready() {
if let Some(text) = result {
self.sample_text = text.clone();
if promise.ready().is_some() {
// Clear promise and take the value out
// Doesn't matter for string as we can just clone it but depending on the type you have
// you may not be able to easily clone it and would prefer get the owned value
let mut temp = None;
std::mem::swap(&mut temp, &mut self.promise);

let owned_promise = temp.expect("we got here because it was some");
let inner_option = owned_promise.block_and_take(); // This should be fine because we know it's ready

if let Some(text) = inner_option {
self.sample_text = text;
} else {
// User probably cancelled or it was saving but the promise completed either way
}
// Clear promise after we use it
self.promise = None;
}
}

Expand Down

0 comments on commit 2a11498

Please sign in to comment.