微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在结构中存储闭包对象?

如何解决如何在结构中存储闭包对象?

我不知道如何在结构中存储闭包对象。闭包对象的参数和返回值是已知的。这是我的精简代码

struct Instr<F>
    where F: Fn([i32;4],[i32;3]) -> [i32;4]
{
    name: String,op: F
}

fn main()
{
    // Simple example showing the difficulty:
    let tmp : Instr<Fn([i32;4],[i32;3]) -> [i32;4]> = Instr { name: "asd".to_string(),op: |a,b| a};

    // What I really want is something more like this:
    // let instrs = vec![
    //     Instr { name: "asdf",b| a },//     Instr { name: "qwer",b| a }
    // ];
}

坦率地说,我不明白这些错误是什么意思。在我看来,这很简单。封闭件具有类型和已知尺寸。将它存储在相同类型的类型字段中应该很简单。对吗?

按照错误消息的提示尝试添加 F: ?Sized 并不能解决“编译时大小未知”错误

有人能帮我正确编译吗?

error[E0277]: the size for values of type `dyn Fn([i32; 4],[i32; 3]) -> [i32; 4]` cannot be kNown at compilation time
  --> a.rs:11:15
   |
1  | struct Instr<F>
   |              - required by this bound in `Instr`
...
11 |     let tmp : Instr<Fn([i32;4],b| a};
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size kNown at compile-time
   |
   = help: the trait `Sized` is not implemented for `dyn Fn([i32; 4],[i32; 3]) -> [i32; 4]`
help: you Could relax the implicit `Sized` bound on `F` if it were used through indirection like `&F` or `Box<F>`
  --> a.rs:1:14
   |
1  | struct Instr<F>
   |              ^ this Could be changed to `F: ?Sized`...
2  |     where F : Fn([i32;4],[i32;3]) -> [i32;4]
   |           - ...if indirection was used here: `Box<F>`
...
5  |     op : F
   |          - ...if indirection was used here: `Box<F>`

error[E0277]: the size for values of type `dyn Fn([i32; 4],[i32; 3]) -> [i32; 4]` cannot be kNown at compilation time
  --> a.rs:11:55
   |
1  | / struct Instr<F>
2  | |     where F : Fn([i32;4],[i32;3]) -> [i32;4]
3  | | {
4  | |     name : String,5  | |     op : F
6  | | }
   | |_- required by `Instr`
...
11 |       let tmp : Instr<Fn([i32;4],b| a};
   |                                                         ^^^^^ doesn't have a size kNown at compile-time
   |
   = help: the trait `Sized` is not implemented for `dyn Fn([i32; 4],[i32; 3]) -> [i32; 4]`

error[E0308]: mismatched types
  --> a.rs:11:92
   |
11 |     let tmp : Instr<Fn([i32;4],b| a};
   |                                                                                            ^^^^^^^ expected trait object `dyn Fn`,found closure
   |
   = note: expected trait object `dyn Fn([i32; 4],[i32; 3]) -> [i32; 4]`
                   found closure `[closure@a.rs:11:92: 11:99]`

解决方法

不同的闭包有不同的大小,所以你不能在结构中存储“原始闭包”或“原始特征对象”,它们必须在一个指针后面,所以你可以像这样把它们放在 Box 中:

struct Instr {
    name: String,op: Box<dyn Fn([i32; 4],[i32; 3]) -> [i32; 4]>,}

fn main() {
    let instrs = vec![
         Instr { name: "asdf".into(),op: Box::new(|a,b| a) },Instr { name: "qwer".into(),b| a) }
     ];
}

playground

,

接受的答案完美地解决了您的用例的解决方案,但我想澄清“未调整大小”的错误消息以及“简单示例”无法正常工作的原因。

Rust 非常有能力将闭包存储在问题中定义的 Instr 中,但是您的类型规范混淆了它。每个闭包的类型都是匿名,你无法命名。您试图通过拼出特征 Fn(ARGS...) -> RESULT 来指定闭包类型的尝试是错误的,因为在 Rust 中,当您在需要类型的地方使用特征时,它指的是特征的动态实现,也就是 {{3} }.它是未定义大小且必须通过引用或智能指针访问的 trait 对象。

因此,如果您让 Rust 推断其类型,您可以创建一个带有任意闭包的 Instr

struct Instr<F>
    where F: Fn([i32;4],[i32;3]) -> [i32;4]
{
    name: String,op: F
}

fn main()
{
    // Simple example
    let tmp : Instr<_> = Instr { name: "asd".to_string(),op: |a,b| a};
}

但是这将不允许您创建每个具有不同闭包的 Instr 向量,因为这些 Instr 将具有不同的类型。为此,您需要使用参考或 Box,如已接受的答案所示。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。