如何解决如果关闭的大小未知,如何将其存储在联合中?
我需要一个联合来存储可能是闭包或变量的结果。问题在于,Rust中的关闭大小在编译时未知,因此下面的联合会阻止我的代码进行编译。
我怎么
- 编译当前代码或
- 找到一种可以是变量或闭包的类型的方法吗?
union Value {
variable: i32,function: dyn Fn(i32) -> i32,}
fn main() {}
error[E0658]: unions with non-`Copy` fields are unstable
--> src/main.rs:1:1
|
1 | / union Value {
2 | | variable: i32,3 | | function: dyn Fn(i32) -> i32,4 | | }
| |_^
|
= note: see issue #55149 <https://github.com/rust-lang/rust/issues/55149> for more information
error[E0277]: the size for values of type `(dyn std::ops::Fn(i32) -> i32 + 'static)` cannot be known at compilation time
--> src/main.rs:3:15
|
3 | function: dyn Fn(i32) -> i32,| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::ops::Fn(i32) -> i32 + 'static)`
= note: no field of a union may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
|
3 | function: &dyn Fn(i32) -> i32,| ^
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
3 | function: Box<dyn Fn(i32) -> i32>,| ^^^^ ^
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。