While using tinyvec I get "field does not implement `Copy`" even though both ArrayVec and Item impl Copy

Viewed 121

Anyone know what's going on here? Why am I getting

this field does not implement Copy

use tinyvec::ArrayVec;

#[derive(Copy, Clone)]
struct Item {
    num: i32
}

#[derive(Copy, Clone)]
struct Test {
    nums: ArrayVec<[Item; 20]>
}

fn main() {

    let mut x = Test {
        nums: ArrayVec::new()
    };

}
1 Answers

Copy is implemented for ArrayVec with the following constraints:

impl<A> Copy for ArrayVec<A> where
    A: Array + Copy,
    A::Item: Copy,
{}

In this case, A is an [Item; 20], so it implements Copy. However, [Item; 20] does not satisfy the constraints to implement Array.

impl<T: Default> Array for [T; 20] {
  // ^^^^^^^^^^

For [Item; 20] to be an Array, Item needs to implement Default.

Related