Pre-RFC: Support deriving traits with long array/tuple fields



Support deriving traits even if a field of the struct/enum is an array of length >32 or a tuple of length >12.


I have an FFI struct

pub struct usbmuxd_device_info_t {
    pub handle: u32,
    pub product_id: c_int,
    pub udid: [c_char; 41],  // <--

which cannot #[derive] any of the built-in trait (Debug, Clone, PartialEq, etc.) because [T; 41] does not implement them. Note that inability to use #[derive(Eq)] means it can’t have the #[structural_match] attribute in stable.

Since #[derive] is just a procedural macro, we could actually inline the logic for [T; 41] without waiting for type-level integers.


“Inline” the derived method when a fixed-length array field [T; n] or a tuple field (T, U, V) is encountered, e.g. currently is expansion is like:

pub struct X<T> {
    a: ([T; 3], i32, T),
    b: Vec<(T, T)>,

// =>

impl <T: ::std::clone::Clone> ::std::clone::Clone for X<T> {
    fn clone(&self) -> X<T> {
        match *self {
            X { a: ref __self_0_0, b: ref __self_0_1 } =>
            X{a: ::std::clone::Clone::clone(&(*__self_0_0)),
              b: ::std::clone::Clone::clone(&(*__self_0_1)),},

We will “inline” the clone method so the expansion becomes:

            X { a: ref __self_0_0, b: ref __self_0_1 } =>
                a: (
                b: ::std::clone::Clone::clone(__self_0_1),


  • The derive logic becomes more complicated: needs to recursively inspect the type of the field and generate different code according to the type.
  • Special casing is not good.


  • Fix RFC issue 1038 (Parameterize types over numerics / type level integers), so [T; $N] can actually implement all those traits. Note that fields with more than 12 tuple fields can still not be derived.

Unsolved questions

  • Should we special case references e.g. &&&&&&&[T; $N] as well?


Deriving for arrays will likely be solved by integer generics.

Tuples… well you probably shouldn’t be using 13-tuples or bigger.


Has there been any headway on integer generics?