1

Is there any crate or functionality that allows me to copy the fields from another structure?

Crate A

struct Product {
    name: String,
    sku: String,
    amount: i32,
}

Crate B

#[derive(copy_fields_from = "a::Product", InputObject)]
struct ProductApi {}

I have to expose several objects from another crate in an API that uses "#[derive(InputObject)]". For that, I have to duplicate all the structures of the other structure. Is it possible to copy the fields of another structure? Perhaps using hypothetical syntax such as #[derive(copy_fields_from ="a::Product")].

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
BeGo
  • 155
  • 8
  • 1
    Does the [newtype pattern](https://doc.rust-lang.org/1.0.0/style/features/types/newtype.html) not work for you? – orlp Nov 18 '20 at 21:47
  • no, because `async_graphql::InputObject ` adds functionality to the structure – BeGo Nov 18 '20 at 22:38

1 Answers1

1

No, there is no way of doing this cleanly.

If you felt you had to do this, you'd need to write a build script that:

  1. Located the source code of the target crate.
  2. Parsed the source code using a crate like syn.
  3. Located the types in question.
  4. Output them again.

See also:


For your specific case, I'd advocate implementing whatever trait(s) by hand.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366