Why does im_rc::Vector .iter() require Copy?

Here, ImVec refers to im_rc::Vector.

In the following code:

pub struct FooBar {}

fn blah(lst: ImVec<FooBar>) {
    let t = lst.iter().map(|x| ());

I get compile error of:

83 |     let t = lst.iter().map(|x| ());
   |                 ^^^^
   = note: the method `iter` exists but the following trait bounds were not satisfied:
           `worp_vm::worp_runtime_err::FooBar : std::clone::Clone`

This confuses me. The map function is passed x: &FooBar as a ref – so why do we need FooBar to have clone ?

It looks like all their data structures require Clone to ever do anything with it.

To be honest I think they should have required Clone only on &mut self methods, since according to their documentation, that’s the only time where actual cloning may happen (Copy-on-Write):

(or at least they should have added a A : Clone bound on the type definitions themselves, so as to make the “you need Clone for absolutely everything” intent clearer)

1 Like