Need to implement trait for Rc<_>?

⚓ Rust    📅 2025-10-02    👤 surdeus    👁️ 7      

surdeus

I'm working on a CLI utility which displays an indicatif::ProgressBar.
I made it possible to pass a custom progress bar by passing a lambda (pb_init) to .with_progress_init, so that in tests I can validate the output.

In a function setting up tests, I define a InMemoryTerm that will be used when drawing the progress bar. I need to define it outside of the pb_init, because I need to return it so I can use it in test assertions.

This is the excerpt of the test code causing trouble (complete code on github):

async fn setup_mocks -> ....... {
// other code
// ....
    let in_mem = Rc::new(InMemoryTerm::new(10, 80));
    let term = in_mem.clone();
    let pb_init = move |size| {
        ProgressBar::with_draw_target(
                Some(size),
                ProgressDrawTarget::term_like(Box::new(*term)))  
//  ----------------------------------------------------^^^^
// 1. cannot move out of an `Rc`
//   move occurs because value has type `indicatif::InMemoryTerm`,
//   which does not implement the `Copy` trait [E0507] 
    };

     let downloader = asfald::Downloader::new()
        .with_client(github_client)
        .with_progress_init(pb_init);

// other code
// ....
    
// fn returns this struct 
    GithubMock {
        cleanup: Box::new(cleanup),
        server_url,
        downloader,
        url,
        expected,
        pb_term: in_mem.clone(),
    }
}

If I dereference it like in the code above, I get the error in comment. If I don't dereference it, I get the error 1. the trait bound std::rc::Rcindicatif::InMemoryTerm: indicatif::TermLike is not satisfied. Does that mean I need to define a struct wrapping the Rc and implement the trait myself for that struct to delegate all calls to trait functions to the Rc value (which is automatically dereferenced)? Is there a trick to make it easy, just as an Rc is automatically dereferenced thanks to the Deref trait?

FYI I'm a recent rust dev, and might have misunderstood something :wink:

3 posts - 2 participants

Read full topic

🏷️ Rust_feed