I have been trying to make this function that takes in a list of IPs (via clientconfig
stuct) and created all clients for the addresses. It should then return these so that I can use them. But I keep getting lifetime errors. I have been trying to understand lifecycles in Rust but I am stuck on this.
I tried:
- Using an 'out' variable like in the example below.
- Normal return type
- Encapsulating in a struct, and setting a property via &mut self.
I just can not get this to work, the current error i am getting is:
> error[E0597]: `completion_queues` does not live long enough
--> src/rdma_client.rs:116:26
|
116 | &completion_queues[i],
| ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
123 | }
| -
| |
| `completion_queues` dropped here while still borrowed
| borrow might be used here, when `out` is dropped and runs the `Drop` code for type `Vec`
|
= note: values in a scope are dropped in the opposite order they are defined
I am running inside Tokio if that matters. The struct clientconfig
and client are mine, but protectiondomain
and context are from the RDMA library.
pub async fn connect_to_clients<'a>(
config: ClientConfig,
ctx: Arc<Context>,
pd: Arc<ProtectionDomain<'_>>,
mut out: Vec<Client<'a>>,
) {
info!("Running client mode.");
let completion_queues: Vec<CompletionQueue> = config
.targets
.iter()
.enumerate()
.map(|(i, _)| ctx.create_cq(1, i.try_into().unwrap()).unwrap())
.collect();
let mut i = 0;
for c in config.targets {
out.push(Client::new(&pd, &completion_queues[i], c.clone(), config.frame_size).await);
i += 1;
}
}
Config.rs:
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct ClientConfig {
pub targets: Vec<String>,
pub frame_size: usize,
}
and client.rs:
pub struct Client<'a> {
qp: QueuePair<'a>,
memory_manager: MemoryManager,
}
finally MemoryManager
:
pub struct MemoryManager {
pub mr: Arc<Mutex<MemoryRegion<u8>>>,
}