Rust 编译时无法知道类型为`(dyn-SeekRead+;';static)`的值的大小
我正在尝试编译以下代码:Rust 编译时无法知道类型为`(dyn-SeekRead+;';static)`的值的大小,rust,Rust,我正在尝试编译以下代码: error[E0277]: the size for values of type `(dyn SeekRead + 'static)` cannot be known at compilation time --> src/archive/zipfile.rs:10:18 | 10 | zip_archive: Box<ZipArchive<dyn SeekRead>>, | ^^
error[E0277]: the size for values of type `(dyn SeekRead + 'static)` cannot be known at compilation time
--> src/archive/zipfile.rs:10:18
|
10 | zip_archive: Box<ZipArchive<dyn SeekRead>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
::: ~/.cargo/registry/src/github.com-1ecc6299db9ec823/zip-0.5.11/src/read.rs:50:23
|
50 | pub struct ZipArchive<R: Read + io::Seek> {
| - required by this bound in `ZipArchive`
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn SeekRead + 'static)`
# cargo.toml
[dependencies]
zip = "0.5.11"
使用zip::read::ZipArchive;
使用std::io::{Read,Seek};
trait SeekRead:Seek+Read{}
pub-struct-ArchiveZip您只需要在结构中使用泛型类型,就像ZipArchive
那样。请注意,ZipArchive
需要一种特定类型:
使用zip::read::ZipArchive;
使用std::io::{Read,Seek};
trait SeekRead:Seek+Read{}
pub struct ArchiveZipHow large你认为dyn SeekRead是什么?ZipArchive
的第一个成员必须是一个。我不明白大小概念是如何结合在一起的。如何将ZipArchive
存储为我的struct
的成员?问题是ZipArchive
不是有效的类型,因为ZipArchive
不可能直接包含dyn SeekRead
。您需要对trait对象使用某种形式的间接寻址,例如ZipArchive