不具有在编译时已知大小的自有结构体的向量

huangapple go评论63阅读模式
英文:

doesn't have a size known at compile-time for Vector of own struct

问题

我正在编写我的第一个Rust程序(在hello world之后)。我创建了一个简单的结构体来包装一个"ImageFile" - 进一步的逻辑将被添加:

use std::path::Path;

pub struct ImageFile {
    size: u64,
    path: Path,
}

首先,我需要将"Path"放在最后一个字段,因为大小当然在运行时是未知的。
我需要这种类型来能够公开有关图像文件的某些"元数据",如类型、扩展名等。

问题是:无论我想在哪里使用 Vec<ImageFile>,我都会得到一个错误,类似于:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
   --> src\image\FileIndex.rs:7:12
    |
7   |     files: Vec<ImageFile>,
    |            ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
    |

这来自于以下代码:

use std::{fs};
use crate::index;
use self::super::ImageFile::ImageFile as ImageFile;

/// 所有给定目录的文件索引
pub struct FileIndex {
    folders: Vec<String>,
    files: Vec<ImageFile>, // 在这里首次出现错误
}

impl FileIndex  {
    pub fn new(folders: Vec<String>) -> FileIndex {
        FileIndex {
            folders,
            files: index(),
        }
    }
    // ...
}

这创建了给定文件夹的所有图像文件的索引。

对于我的用例,最好的修复方法是什么?

英文:

I am writing my first Rust program (after hello world). I created a simple struct to wrap an "ImageFile" - further logic will be added:

use std::path::Path;

pub struct ImageFile {
    size: u64,
    path: Path,
}

First i needed to put "Path" as last field. Because size - is of course - not known at runtime.
I need the type to be able to expose certain "meta data" about an image file, like type, extension etc.

The problem is: wherever i want to use Vec&lt;ImageFile&gt; i get an error like

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
   --&gt; src\image\FileIndex.rs:7:12
    |
7   |     files: Vec&lt;ImageFile&gt;,
    |            ^^^^^^^^^^^^^^ doesn&#39;t have a size known at compile-time
    |

this is from

use std::{fs};
use crate::index;
use self::super::ImageFile::ImageFile as ImageFile;
/// index of all files for all given dircetories
pub struct FileIndex {
    folders: Vec&lt;String&gt;,
    files: Vec&lt;ImageFile&gt;, //first error here
}

impl FileIndex  {
    pub fn new(folders: Vec&lt;String&gt;) -&gt; FileIndex {
        FileIndex {
            folders,
            files: index(),
        }
    }
...

This creates an index of all image files for the given folders.

What is the best way to fix this for my use case?

答案1

得分: 1

这是我能找到的最好解释。

如文章中所述,Path 实现 仅适用于对于不定大小类型的引用。

只有在你想要改变路径的情况下,才有意义使用 PathBuf。

你能做的最好的事情是将 ImageFile 结构定义如下。

pub struct ImageFile<'a> {
    size: u64,
    path: &'a Path,
}
英文:

This is the best explanation I can find.

As explained in the article Path Implementation works only for references for unsized types.

Using PathBuf only makes sense if you want to mutate the path for some reason.

The best you can do is defining the ImageFile struct as follows.

pub struct ImageFile &lt;&#39;a&gt; {
    size: u64,
    path: &amp;&#39;a Path,
}

huangapple
  • 本文由 发表于 2023年7月24日 19:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定