Skip to content

Commit 2a11498

Browse files
committed
Remove clone of value in the promise
1 parent 93f4e46 commit 2a11498

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/app.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,21 @@ impl eframe::App for BrowseApp {
5858
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
5959
// assign sample text once it comes in
6060
if let Some(promise) = &self.promise {
61-
if let Some(result) = promise.ready() {
62-
if let Some(text) = result {
63-
self.sample_text = text.clone();
61+
if promise.ready().is_some() {
62+
// Clear promise and take the value out
63+
// Doesn't matter for string as we can just clone it but depending on the type you have
64+
// you may not be able to easily clone it and would prefer get the owned value
65+
let mut temp = None;
66+
std::mem::swap(&mut temp, &mut self.promise);
67+
68+
let owned_promise = temp.expect("we got here because it was some");
69+
let inner_option = owned_promise.block_and_take(); // This should be fine because we know it's ready
70+
71+
if let Some(text) = inner_option {
72+
self.sample_text = text;
6473
} else {
6574
// User probably cancelled or it was saving but the promise completed either way
6675
}
67-
// Clear promise after we use it
68-
self.promise = None;
6976
}
7077
}
7178

0 commit comments

Comments
 (0)