Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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()
    };

}
like image 863
Owen Doyle Avatar asked Dec 18 '22 11:12

Owen Doyle


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.

like image 150
kmdreko Avatar answered Jun 08 '23 23:06

kmdreko