How to get RefCell to an object and Rc to its part - rust

I need to call a mut method on an object from one callback and another method from another callback (a timer and request animation frame in a webasm thing) on its field returned from that object. Whatever I tried I was getting various errors, mostly like 'creates a temporary which is freed while still in use'. Is it ever possible?
let mut top = Top {
item: Item {}
};
let top_rc_refcell = Rc::new(RefCell::new(top));
let top_rc_1 = Rc::clone(&top_rc_refcell);
let box_1 = Box::new(move || {
top_rc_1.borrow_mut().some_mut();
});
let top_rc_2 = Rc::clone(&top_rc_refcell);
//ref_to_item is costly so calling it from within callback is not an option
let item_rc = Rc::new(&top_rc_2.borrow().ref_to_item());
let item_rc_in_box = Rc::clone(&item_rc);
let box_2 = Box::new(move || {
item_rc_in_box.some();
});
(box_1)();
(box_2)();
Errors:
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:46:28
|
46 | let item_rc = Rc::new(&top_rc_2.borrow().ref_to_item());
| ^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
47 |
48 | let item_rc_in_box = Rc::clone(&item_rc);
| -------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:46:28
|
46 | let item_rc = Rc::new(&top_rc_2.borrow().ref_to_item());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
47 |
48 | let item_rc_in_box = Rc::clone(&item_rc);
| -------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
playground

Not exactly. You cannot create a Rc pointing to the field because the field will have to be wrapped somehow in the Rc. You can only do so for a pointer to the field, and this is pretty useless and also the source of your errors.
You need to either declare the field as Rc or just use the whole-object Rc.

Related

temporary value dropped while borrowed and cannot resolve

I am trying to sending data to an API endpoint and running into some borrowing issuess
Code
#[post("/send")]
async fn send(field: web::Json<Region>, data: web::Data<Datum>) -> impl Responder {
let address = data.address;
let provider = Provider::try_from(&data.provider).unwrap();
let provider = Arc::new(provider);
let coordinate = Coordinate::new(address, provider);
let response = coordinate
.method::<_, (Vec<U256>, U256)>("send", (field.region, field.price))
.unwrap()
.send()
.await;
HttpResponse::Created()
.content_type(ContentType::json())
.body(response.unwrap().to_string())
}
Error
error[E0716]: temporary value dropped while borrowed
--> app/src/main.rs:132:14
|
132 | let response = land_contract
| ______________^
133 | | .method::<_, (Vec<U256>, U256)>("send", (field.region, field.price))
134 | | .unwrap()
| |_________________^ creates a temporary which is freed while still in use
135 | .send()
136 | .await;
| - temporary value is freed at the end of this statement
...
141 | }
| - borrow might be used here, when `tx` is dropped and runs the destructor`
|
= note: consider using a `let` binding to create a longer lived value
error[E0507]: cannot move out of dereference of `Json<Region>`
--> app/src/main.rs:133:50
|
133 | .method::<_, (Vec<U256>, U256)>("send", (field.region, field.price))
| ^^^^^^^^^^^^ move occurs because value has type `Vec<U256>`, which does not implement the `Copy` trait
.method::<_, (Vec, U256)>("send", (field.region, field.price))
^^^^^^^^^^^^ move occurs because value has type Vec<U256>, which does not implement the Copy trait
The error is that field.region is a Vec<U256> which doesn't implement a Copy trait. Since that's the case, the compiler is trying to move the value out as mentioned in the error cannot move out of dereference of Json. One way to fix this issue is cloning the value of field.region`.
.method::<_, (Vec<U256>, U256)>("send", (field.region.clone(), field.price))
You might have to do similar cloning for field.price also.
The other error is regarding temporary value being created at .unwrap(). Since you are using await. It will wait for the future to complete but the value created with unwrap() gets dropped as it is not bound to any local variable so you get this error.
Break the code into 2 lines to solve this error with help of a local binding.
let response = coordinate
.method::<_, (Vec<U256>, U256)>("send", (field.region, field.price))
.unwrap();
response.send().await;

cannot move out of `guess`, a captured variable in an `Fn` closure Rust

today I tried to write this MusicGuesser with GTK for fun and got this error:
error[E0507]: cannot move out of `guess`, a captured variable in an `Fn` closure
--> src/main.rs:63:32
|
15 | let mut guess = Arc::new(get_guess());
| --------- captured outer variable
16 |
17 | app.connect_activate(move |app| {
| ---------- captured by this `Fn` closure
...
63 | button.connect_clicked(move |_| {
| ^^^^^^^^ move out of `guess` occurs here
64 | let mut guess = Arc::clone(&guess);
| -----
| |
| variable moved due to use in closure
| move occurs because `guess` has type `Arc<Vec<std::string::String>>`, which does not implement the `Copy` trait
I found some same questions about this error, but I didn't understand them. There is source code: https://pastebin.com/1pNxEiB5
let guess = Arc::new(get_guess());
let cloned_guess = Arc::clone(&guess);
// ...
app.connect_activate(move |app| {
// ...
button.connect_clicked(move |_| {
let guess = cloned_guess;
// ...
If you use something in a move || closure, you move it into that closure. That means in your case you move the entire outer guess object in.
So you need to clone first, and then only move the cloned object in.
You will hit the next problem soon, though, because the content of Arc is always immutable. In Rust, you can never have multiple mutable references to the same thing. So in order to modify your guess, you will have to create interior mutability via Mutex or similar.

Referencing a cell in multiple closures with Cursive

I am using the Cursive TUI crate and I am having trouble with a dialog button because I want to set an input to change with the dialog response. This is my code:
fn prompt_repeat(siv: &mut Cursive) -> bool {
let input = Cell::new(false);
siv.add_layer(views::Dialog::text("Do you want to retype the note again?")
.title("Redo?")
.button("Yes", |s| {
input.set(true);
s.quit();
})
.button("No", |s| {
s.quit();
}));
siv.run();
input.get()
}
This causes error E0373:
error[E0373]: closure may outlive the current function, but it borrows `input`, which is owned by the current function
--> src/main.rs:94:23
|
94 | .button("No", |s| {
| ^^^ may outlive borrowed value `input`
95 | input.set(false)
| ----- `input` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src/main.rs:89:19
|
89 | siv.add_layer(views::Dialog::text("Do you want to retype the note again?")
| ___________________^
90 | | .title("Redo?")
91 | | .button("Yes", |s| {
92 | | input.set(true)
... |
95 | | input.set(false)
96 | | })
| |______________^
help: to force the closure to take ownership of `input` (and any other referenced variables), use the `move` keyword
|
94 | .button("No", move |s| {
| ++++
I understand that this is caused because the compiler cannot know if input will still exist before the closure uses it, but I cannot use move because I need to reference the cell again to return it (I have tried this and the compiler tells me I have borrowed the value multiple times, which is correct).
How should I go about referencing the cell in multiple closures?
I agree with #Jmb, I'd probably go with a Rc reference counting smart pointer:
fn prompt_repeat(siv: &mut Cursive) -> bool {
let input = Rc::new(Cell::new(false));
let input_2 = input.clone();
siv.add_layer(
views::Dialog::text("Do you want to retype the note again?")
.title("Redo?")
.button("Yes", move |s| {
input_2.set(true);
s.quit();
})
.button("No", move |s| {
s.quit();
}),
);
siv.run();
input.get()
}

Mutate an object which was taken from a vector

I have trouble solving an obvious simple problem.
Basically I want to push an instance of a structure to a vector to get it out later and to modify the object by calling a function implemented for the structure.
To simplify the case, I created the following test code, which reflects in my opinion to the same problem.
let mut strings = Vec::new();
strings.push("Hello".to_string());
let string_option = strings.last();
let string = string_option.unwrap();
string.shrink_to(1);
This has the compile error
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
--> src/main.rs:89:5
|
88 | let string = string_option.unwrap();
| ------ help: consider changing this to be a mutable reference: `&mut String`
89 | string.shrink_to(1);
| ^^^^^^^^^^^^^^^^^^^ `string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
Then I tried sheer endless variants like
let mut strings = Vec::new();
strings.push("Hello".to_string());
let string_option = strings.last().as_mut();
let string = string_option.unwrap();
string.shrink_to(1);
... or ...
let mut strings = Vec::new();
strings.push("Hello".to_string());
let string_option = strings.last().as_deref_mut();
let string = string_option.unwrap();
string.shrink_to(1);
Actually the code shown above is a simplification from this code, which I originally wanted to do.
struct Bar {
data: Vec<String>
}
impl Bar {
fn shrink_first(&mut self) {
let s_opt = self.data.last().as_mut(); // s_opt is of type Option<&mut &String>
let s = s_opt.unwrap(); // s is of type &mut & String
s.shrink_to(1);
}
}
The code above brings the following errors ...
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:67:21
|
67 | let s_opt = self.data.last().as_mut(); // s_opt is of type Option<&mut &String>
| ^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
68 |
69 | let s = s_opt.unwrap(); // s is of type &mut & String
| ----- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error[E0596]: cannot borrow `**s` as mutable, as it is behind a `&` reference
--> src/main.rs:70:9
|
70 | s.shrink_to(1);
| ^^^^^^^^^^^^^^ cannot borrow as mutable
But in my opinion it always has the same root causes, but I have not figured out what to do.
Simply change strings.last() to strings.last_mut().
The last() method returns a standard (immutable) reference (to be more precise, Option<&T>).
To be able to mutate the last String in the vector, you need to get a mutable reference via last_mut().

Spawning threads and keeping data from them

This is a super contrived example that shows what I want to do; I have a HashMap that I want to push data in to, but I want to do it in a set of threads - the real example is that I'm connecting to a remote service inside the thread::spawn and so I want to have all of it happen in the background as much as possible so it doesn't block the primary thread.
I'm not sure how to refactor my code to allow what I'm wanting to do - any suggestions would be helpful!
let mut item_array : HashMap<i32, i32> = HashMap::new();
let mut array: [i32; 3] = [0, 1, 2];
for item in &array {
thread::spawn(|| {
item_array.insert(*item, *item);
});
}
println!("{:?}", item_array);
The errors I receive are below
error[E0597]: `array` does not live long enough
--> src/main.rs:87:17
|
87 | for item in &array {
| ^^^^^^
| |
| borrowed value does not live long enough
| argument requires that `array` is borrowed for `'static`
...
153 | }
| - `array` dropped here while still borrowed
error[E0499]: cannot borrow `item_array` as mutable more than once at a time
--> src/main.rs:88:23
|
88 | thread::spawn(|| {
| - ^^ mutable borrow starts here in previous iteration of loop
| _________|
| |
89 | | item_array.insert(*item, *item);
| | ---------- borrows occur due to use of `item_array` in closure
90 | | });
| |__________- argument requires that `item_array` is borrowed for `'static`
error[E0373]: closure may outlive the current function, but it borrows `item`, which is owned by the current function
--> src/main.rs:88:23
|
88 | thread::spawn(|| {
| ^^ may outlive borrowed value `item`
89 | item_array.insert(*item, *item);
| ---- `item` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src/main.rs:88:9
|
88 | / thread::spawn(|| {
89 | | item_array.insert(*item, *item);
90 | | });
| |__________^
help: to force the closure to take ownership of `item` (and any other referenced variables), use the `move` keyword
|
88 | thread::spawn(move || {
| ^^^^^^^
error[E0373]: closure may outlive the current function, but it borrows `item_array`, which is owned by the current function
--> src/main.rs:88:23
|
88 | thread::spawn(|| {
| ^^ may outlive borrowed value `item_array`
89 | item_array.insert(*item, *item);
| ---------- `item_array` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src/main.rs:88:9
|
88 | / thread::spawn(|| {
89 | | item_array.insert(*item, *item);
90 | | });
| |__________^
help: to force the closure to take ownership of `item_array` (and any other referenced variables), use the `move` keyword
|
88 | thread::spawn(move || {
| ^^^^^^^
error[E0502]: cannot borrow `item_array` as immutable because it is also borrowed as mutable
--> src/main.rs:92:22
|
88 | thread::spawn(|| {
| - -- mutable borrow occurs here
| _________|
| |
89 | | item_array.insert(*item, *item);
| | ---------- first borrow occurs due to use of `item_array` in closure
90 | | });
| |__________- argument requires that `item_array` is borrowed for `'static`
91 | }
92 | println!("{:?}", item_array);
| ^^^^^^^^^^ immutable borrow occurs here
There are two problems here (which are typical problems with multithreaded Rust):
Your thread cannot borrow any data that may outlive it (which, when using std::thread::spawn, is any data1).
Multiple thread cannot borrow the same mutable data.
The first problem is typically solved by:
Copying data instead of referencing it. This is particularly useful for primitive types, such as integers.
Using scoped threads.
Using Arc for thread-safe shared pointer, to ensure that data outlives all threads using it.
Not all are possible in all cases, but I'd recommend the above solutions in that order.
The second problem is typically solved with locks such as Mutex or RwLock, which allows only a single thread to get a mutable reference at a time.
Combining these, I'd solve your example like this:
// mutable data is wrapped in a Mutex
let item_array: Mutex<HashMap<i32, i32>> = Mutex::new(HashMap::new());
// immutable data does not have to be wrapped with scoped threads
let array: [i32; 3] = [0, 1, 2];
// using crossbeam::scope from the crossbeam library, which lets
// us reference variables outside the scope (item_array and array)
crossbeam::scope(|s| {
for item in &array {
// copy item (since it is an integer)
let item = *item;
// explicitly reference item_array, since we'll later need to move this
let item_array_ref = &item_array;
// move item and item_array_ref into the closure (instead of referencing,
// which is by default)
s.spawn(move |_| {
// need to call .lock() to aquire a mutable reference to the HashMap
// will wait until the mutable reference is not used by any other threads
let mut item_array_lock = item_array_ref.lock().unwrap();
item_array_lock.insert(item, item);
});
}
// the scope will persist until all threads spawned inside it (with s.spawn) have completed, blocking the current thread,
// ensuring that any referenced data outlives all the threads
})
.unwrap();
// need to call .lock() here as well to get a reference to the HashMap
println!("{:?}", item_array.lock().unwrap());
Run in playground
1Except when the data has 'static lifetime, i.e. can exist for the entire lifetime of the program.
Here is very simple example of code based on source you have provieded:
use std::collections::HashMap;
use std::thread;
fn main() {
let item_array: HashMap<i32, i32> = HashMap::new();
let array: [i32; 3] = [0, 1, 2];
let mut handles = vec![];
let mutex = std::sync::Mutex::new(item_array);
let arc = std::sync::Arc::new(mutex);
for item in &array {
let item = item.to_owned();
let arc_cloned = std::sync::Arc::clone(&arc);
let th = thread::spawn(move || {
let mut guard = arc_cloned.lock().unwrap();
guard.insert(item, item);
});
handles.push(th);
}
for handle in handles {
handle.join().unwrap();
}
println!("{:?}", arc.lock().unwrap());
}
And you can play it here on rust playground

Resources