实现 Rust 结构时如何处理生命周期?
How do I deal with lifetimes when implementing rust structs?
所以,我一直在浏览 Handmade Hero 视频系列,我认为将代码转换为 Rust 会是一个有趣的练习。但是,我以前从来没有真正需要处理生命周期,现在我遇到了与它们相关的编译错误。我目前有以下代码:
struct TextureData<'a> {
texture: Texture<'a>,
width: usize,
height: usize,
bytes_per_pixel: usize,
pixels: Vec<u8>
}
impl TextureData<'_> {
fn new(texture: Texture, width: usize, height: usize) -> Self {
TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
}
fn fill_texture(&mut self) {
let size = self.width * self.height * self.bytes_per_pixel;
let mut pixels: Vec<u8> = Vec::with_capacity(size);
for i in 0..(self.width * self.height){
let x = i / self.width;
let y = i % self.height;
pixels.push((x % 0xff) as u8);
pixels.push((y % 0xff) as u8);
pixels.push(0);
pixels.push(0);
}
self.pixels = pixels;
}
fn update(&mut self) {
let pitch = self.width * self.bytes_per_pixel;
self.texture.update(None, &self.pixels, pitch);
}
}
我收到了以下反馈:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/main.rs:21:2
|
21 | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
| ^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 20:5...
--> src/main.rs:20:5
|
20 | / fn new(texture: Texture, width: usize, height: usize) -> Self {
21 | | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
22 | | }
| |_____^
note: ...so that the expression is assignable
--> src/main.rs:21:16
|
21 | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
| ^^^^^^^
= note: expected `sdl2::render::Texture<'_>`
found `sdl2::render::Texture<'_>`
note: but, the lifetime must be valid for the lifetime `'_` as defined on the impl at 19:18...
--> src/main.rs:19:18
|
19 | impl TextureData<'_> {
| ^^
note: ...so that the expression is assignable
--> src/main.rs:21:2
|
21 | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `TextureData<'_>`
found `TextureData<'_>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
当然,我已经阅读了 E0495 的文档,但我不知道它如何适用于这种情况。
您需要像声明 TextureData
结构时那样重复它:
impl<'a> TextureData<'a> {
fn new(texture: Texture<'a>, width: usize, height: usize) -> Self {
...
}
...
}
注意添加的 3 <'a>
。它不一定是 'a
,也可以在这 3 个地方是 'b
。
或者,如果你想使用"anonymous lifetime"形式,那么你需要这样写。
impl TextureData<'_> {
fn new(texture: Texture<'_>, width: usize, height: usize) -> TextureData<'_> {
...
}
...
}
所以,我一直在浏览 Handmade Hero 视频系列,我认为将代码转换为 Rust 会是一个有趣的练习。但是,我以前从来没有真正需要处理生命周期,现在我遇到了与它们相关的编译错误。我目前有以下代码:
struct TextureData<'a> {
texture: Texture<'a>,
width: usize,
height: usize,
bytes_per_pixel: usize,
pixels: Vec<u8>
}
impl TextureData<'_> {
fn new(texture: Texture, width: usize, height: usize) -> Self {
TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
}
fn fill_texture(&mut self) {
let size = self.width * self.height * self.bytes_per_pixel;
let mut pixels: Vec<u8> = Vec::with_capacity(size);
for i in 0..(self.width * self.height){
let x = i / self.width;
let y = i % self.height;
pixels.push((x % 0xff) as u8);
pixels.push((y % 0xff) as u8);
pixels.push(0);
pixels.push(0);
}
self.pixels = pixels;
}
fn update(&mut self) {
let pitch = self.width * self.bytes_per_pixel;
self.texture.update(None, &self.pixels, pitch);
}
}
我收到了以下反馈:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/main.rs:21:2
|
21 | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
| ^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 20:5...
--> src/main.rs:20:5
|
20 | / fn new(texture: Texture, width: usize, height: usize) -> Self {
21 | | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
22 | | }
| |_____^
note: ...so that the expression is assignable
--> src/main.rs:21:16
|
21 | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
| ^^^^^^^
= note: expected `sdl2::render::Texture<'_>`
found `sdl2::render::Texture<'_>`
note: but, the lifetime must be valid for the lifetime `'_` as defined on the impl at 19:18...
--> src/main.rs:19:18
|
19 | impl TextureData<'_> {
| ^^
note: ...so that the expression is assignable
--> src/main.rs:21:2
|
21 | TextureData { texture, width, height, bytes_per_pixel: PIXEL_BYTES, pixels: Vec::new() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: expected `TextureData<'_>`
found `TextureData<'_>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.
当然,我已经阅读了 E0495 的文档,但我不知道它如何适用于这种情况。
您需要像声明 TextureData
结构时那样重复它:
impl<'a> TextureData<'a> {
fn new(texture: Texture<'a>, width: usize, height: usize) -> Self {
...
}
...
}
注意添加的 3 <'a>
。它不一定是 'a
,也可以在这 3 个地方是 'b
。
或者,如果你想使用"anonymous lifetime"形式,那么你需要这样写。
impl TextureData<'_> {
fn new(texture: Texture<'_>, width: usize, height: usize) -> TextureData<'_> {
...
}
...
}