Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there syntax for moving fields between similar structs?

Tags:

syntax

rust

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?

like image 487
FauxFaux Avatar asked Jun 21 '18 07:06

FauxFaux


1 Answers

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:

  • RFC 2528 — Type-changing struct update syntax
  • Issue #47741 — Struct initializer ..x syntax should work for other structs with structurally equal subset of fields
  • Similar issue: Struct update syntax for different types
like image 106
Shepmaster Avatar answered Nov 02 '22 05:11

Shepmaster