My goal is to write a function that will take an Rc<RefCell<&'a mut [u8]>>
as an argument and return a struct that contains a reference to the inner slice, but I can't satisfy the borrow checker. My first attempt looked like this:
pub fn mk_buf_holder<'a>(buf: Rc<RefCell<&'a mut [u8]>>) -> BufHolder<'a> {
BufHolder::<'a> { buf: buf.borrow_mut().deref_mut()}
}
But that doesn't work, of course, because the result of borrow_mut
goes out of scope. My next attempt added extra members to the struct, just to keep around values that would otherwise be temporary. I thought that putting them into the struct would give them the same lifetime as buf
, but the borrow checker disagrees. Here's the full example:
use std::cell::{Ref, RefCell, RefMut};
use std::ops::DerefMut;
use std::rc::Rc;
pub struct BufHolder<'a> {
buf: &'a mut [u8],
bufclone: Rc<RefCell<&'a mut [u8]>>,
bufref: RefMut<'a, &'a mut[u8]>
}
pub fn mk_buf_holder<'a>(buf: Rc<RefCell<&'a mut [u8]>>) -> BufHolder<'a> {
let bufclone = buf.clone();
let bufref = bufclone.borrow_mut();
BufHolder::<'a> { bufclone: bufclone,
buf: bufref.deref_mut(),
bufref: bufref }
}
The compiler still tells me that the result of borrow_mut()
doesn't live long enough, even though I added it to the output structure. It's as if the compiler is copying into the output, instead of moving it. How can I fix this function?
error: `bufclone` does not live long enough
--> src/main.rs:13:18
|
13 | let bufref = bufclone.borrow_mut();
| ^^^^^^^^ does not live long enough
...
19 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 11:74...
--> src/main.rs:11:75
|
11 | pub fn mk_buf_holder<'a>(buf: Rc<RefCell<&'a mut [u8]>>) -> BufHolder<'a> {
| ___________________________________________________________________________^ starting here...
12 | | let bufclone = buf.clone();
13 | | let bufref = bufclone.borrow_mut();
14 | | BufHolder::<'a> {
15 | | bufclone: bufclone,
16 | | buf: bufref.deref_mut(),
17 | | bufref: bufref,
18 | | }
19 | | }
| |_^ ...ending here
error: `bufref` does not live long enough
--> src/main.rs:16:14
|
16 | buf: bufref.deref_mut(),
| ^^^^^^ does not live long enough
...
19 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 11:74...
--> src/main.rs:11:75
|
11 | pub fn mk_buf_holder<'a>(buf: Rc<RefCell<&'a mut [u8]>>) -> BufHolder<'a> {
| ___________________________________________________________________________^ starting here...
12 | | let bufclone = buf.clone();
13 | | let bufref = bufclone.borrow_mut();
14 | | BufHolder::<'a> {
15 | | bufclone: bufclone,
16 | | buf: bufref.deref_mut(),
17 | | bufref: bufref,
18 | | }
19 | | }
| |_^ ...ending here