Borrowing issue after wrapping the logic into a function

⚓ Rust    📅 2025-07-07    👤 surdeus    👁️ 3      

surdeus

I had following code (partly, cli.db was not used after this part) that built, in which cli is a struct, db is PathBuf.

            let h = tokio::spawn({
                tracing::debug!("CLI interfacer starts");
                let uds = tarpc::serde_transport::unix::listen(&cli.uds, Bincode::default).await?;
                let mut perm = fs::metadata(&cli.uds).await?.permissions();
                perm.set_mode(perm.mode() | 0o222); // chmod a+w
                fs::set_permissions(&cli.uds, perm).await?;

Then I wrapped the inner block into a function:

async fn serv_cli(uds: &Path, server: Server) -> Result<()> {
    tracing::debug!("CLI interfacer starts");
    let listener = tarpc::serde_transport::unix::listen(uds, Bincode::default).await?;
    let mut perm = fs::metadata(uds).await?.permissions();
    perm.set_mode(perm.mode() | 0o222); // chmod a+w
    fs::set_permissions(uds, perm).await?;

And

let h = tokio::spawn(serv_cli(
                &cli.uds,
                Server {
                    db: cli.db,
                    inotify: inotify.clone(),
                    records: records.clone(),
                },
            ));

Now it breaks, "cli.uds" does not live long enough.

I could fix this by clone or other solutions. But I wonder why it worked before. What is the difference here?

4 posts - 2 participants

Read full topic

🏷️ rust_feed