FauxFaux
FauxFaux

Reputation: 2515

Is there syntax for moving fields between similar structs?

I have a big struct Foo<Q>, and want to map it into a Foo<R> where most of the fields don't need updating. I was hoping to use the .. operator for this, but it is a type error, as they are technically different types.

That is, given:

struct Foo<T> {
    a: usize,
    b: usize,
    t: T,
}

let q: Foo<Q>;

I would like to write:

let r = Foo::<R> {
    t: fixup(q.t),
    ..q
};

But, this gives me a type error:

error[E0308]: mismatched types
   |
 3 |         ..q
   |           ^ expected struct `R`, found struct `Q`
   |
   = note: expected type `Foo<R>`
              found type `Foo<Q>`

The type error is reasonable, as the types can be thought of as templates in this case.

The only workaround I have is to write out the transformation in full, which gets ugly quite quickly:

let r = Foo::<R> {
    a: q.a,
    b: q.b,
    t: fixup(q.t),
};

Here's a playground with a full test-case, including the compile error and the long-form.


Is there better syntax for this somewhere, or a better way to implement these map-like methods for non-trivial structs?

Upvotes: 11

Views: 2121

Answers (1)

Shepmaster
Shepmaster

Reputation: 430554

Is there syntax for moving fields between similar structs?

No. There is no such syntax. The current implementation of "struct update" (previously called "functional record update") syntax only allows the exact same type.

Is there better syntax for this somewhere, or a better way to implement these map-like methods for non-trivial structs?

No. The only suggestion I have is to destructure your original struct and then recreate it. You also don't need the ::<R> as it's inferred.

let Foo { a, b, c, d, e, t } = q;
let r = Foo {
    a,
    b,
    c,
    d,
    e,
    t: fixup(t),
};

See also:

Upvotes: 13

Related Questions