Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reference 生命周期和对包含引用的对象的引用_Reference_Rust_Lifetime - Fatal编程技术网

Reference 生命周期和对包含引用的对象的引用

Reference 生命周期和对包含引用的对象的引用,reference,rust,lifetime,Reference,Rust,Lifetime,假设我有一个结构中有一个引用,另一个结构中有一个引用,类似这样: struct Image<'a> { pixel_data: &'a mut Vec<u8>, size: (i32, i32), } struct SubImage<'a> { image: &'a mut Image<'a>, offset: (i32, i32), size: (i32, i32), } fn rend

假设我有一个结构中有一个引用,另一个结构中有一个引用,类似这样:

struct Image<'a> {
    pixel_data: &'a mut Vec<u8>,
    size: (i32, i32),
}

struct SubImage<'a> {
    image: &'a mut Image<'a>,
    offset: (i32, i32),
    size: (i32, i32),
}
fn render2(image: &mut Image) {
    let mut sub = SubImage {
        image: image,           // line 62
        offset: (100, 100),
        size: (600, 400),
    };

    sub.rect_fill(0, 0, 10, 10);
}
然后我创建了
子图像
,并想做如下事情:

struct Image<'a> {
    pixel_data: &'a mut Vec<u8>,
    size: (i32, i32),
}

struct SubImage<'a> {
    image: &'a mut Image<'a>,
    offset: (i32, i32),
    size: (i32, i32),
}
fn render2(image: &mut Image) {
    let mut sub = SubImage {
        image: image,           // line 62
        offset: (100, 100),
        size: (600, 400),
    };

    sub.rect_fill(0, 0, 10, 10);
}
但是,这会导致编译器错误:

main.rs:62:16: 62:21 error: cannot infer an appropriate lifetime for automatic coercion due to conflicting requirements
编译器建议将签名更改为:

fn render2<'a>(image: &'a mut Image<'a>)

fn render2是的,这个错误可能会让人困惑,但它有一个合理的原因

struct SubImage<'a> {
    image: &'a mut Image<'a>,
    offset: (i32, i32),
    size: (i32, i32),
}
因此,它尝试使用
&'b mut Image创建
子映像
我可以对子映像做些什么,使这些显式生命周期不必要吗

弗拉基米尔的回答很贴切,但我鼓励您稍微修改一下代码。我的许多原始代码都有非常相似的引用,这些引用都带有引用如果你需要,那么分开生活会有很大帮助。但是,我只是将
图像
嵌入
子图像

struct Image<'a> {
    pixel_data: &'a mut Vec<u8>,
    size: (i32, i32),
}

struct SubImage<'a> {
    image: Image<'a>,
    offset: (i32, i32),
    size: (i32, i32),
}
struct-Image,
偏移量:(i32,i32),
尺寸:(i32,i32),
}

在我的例子中,通过嵌套引用,我并没有真正获得任何东西。直接嵌入结构会使它变大一点,但可以使访问速度加快一点(减少一次指针追逐)。重要的是,在这种情况下,它不再需要第二个生存期。

关于生存期界限有任何官方信息吗?看起来对我来说是非常重要的信息,但我没有在官方书籍或规范中看到它。事实上,这是一件重要的事情,我认为我也没有在文档中看到它。在Rust issue tracker中创建一个问题可能是值得的。在我的实际代码中,
pixel\u数据
实际上是由不同的结构所拥有,不同的模块因平台而异。此结构可能包含其他数据(例如,在使用Windows GDI时,它包含调用
StretchDIBits
所需的
BITMAPINFO
对象)。这个特定于平台的代码调用我的代码,给我一个
图像
对象,其中包含对其像素数据的引用。我可以消除中间人(
Image
),让特定于平台的代码只传递给我一个实际代表整个图像的
SubImage
。但到目前为止,我的代码就是这样发展的。@BenjaminLindley如果
pixel\u数据
属于不同的结构,那就好了。注意,最终的代码仍然有一个引用,并且没有试图拥有它。我的观点是,你很少需要一个对一个引用的引用。请注意,您可以轻松复制
图像
,这表明您可以将其嵌入。听起来你真的可以按照我的建议去做,所以我鼓励你尝试一下!哇,我在阅读答案时从来没有这么不注意过。我想是累了吧。出于某种原因,我以为你说的是在
图像中嵌入像素数据。我一定是刚读过“我的很多原始代码都有非常相似的引用,引用的东西都有引用。”然后我假设这就是你要去的地方,然后我的大脑填满了你剩下的答案。不管怎样,是的,那可能行得通。
fn render2<'a>(image: &'a mut Image<'a>)
struct SubImage<'b, 'a:'b> {
    image: &'b mut Image<'a>,
    offset: (i32, i32),
    size: (i32, i32),
}
struct Image<'a> {
    pixel_data: &'a mut Vec<u8>,
    size: (i32, i32),
}

struct SubImage<'a> {
    image: Image<'a>,
    offset: (i32, i32),
    size: (i32, i32),
}