简体   繁体   English

如何复制另一个结构的字段?

[英]How can I copy the fields of another structure?

Is there any crate or functionality that allows me to copy the fields from another structure?是否有任何板条箱或功能允许我从另一个结构复制字段?

Crate A板条箱 A

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

Crate B板条箱 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)] ".我必须在使用“ #[derive(InputObject)] ”的 API 中从另一个板条箱中公开几个对象。 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")] .也许使用假设的语法,例如#[derive(copy_fields_from ="a::Product")]

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 .使用像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.对于您的具体情况,我主张手动实现任何特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM