gpt4 book ai didi

rust - 由于需求冲突,无法推断出 autoref 的适当生命周期

转载 作者:行者123 更新时间:2023-11-29 08:33:22 24 4
gpt4 key购买 nike

我的代码中的某个特定函数存在生命周期问题。我正在按照教程尝试学习 Rust 和 SDL。该教程稍旧一些,SDL 库自编写以来发生了变化,因此我将继续学习,同时也将其调整为最新版本的 Rust-SDL。

生命周期问题在这个函数中:

pub fn ttf_str_sprite(&mut self, text: &str, font_path: &'static str, size: i32, color: Color) -> Option<Sprite> {
if let Some(font) = self.cached_fonts.get(&(font_path, size)) {
return font.render(text).blended(color).ok()
.and_then(|surface| self.renderer.create_texture_from_surface(&surface).ok())
.map(Sprite::new)
}
//::sdl2_ttf::Font::from_file(Path::new(font_path), size).ok()
self.ttf_context.load_font(Path::new(font_path), size as u16).ok()
.and_then(|font| {
self.cached_fonts.insert((font_path, size), font);
self.ttf_str_sprite(text, font_path, size, color)
})
}

特别是行 self.ttf_context.load_font(Path::new(font_path), size as u16).ok()。上面的注释行是老SDL版本的字体加载方式。

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src\phi/mod.rs:57:26
|
57 | self.ttf_context.load_font(Path::new(font_path), size as u16).ok()
| ^^^^^^^^^
|
help: consider using an explicit lifetime parameter as shown: fn ttf_str_sprite(&'window mut self, text: &str, font_path: &'static str,
size: i32, color: Color) -> Option<Sprite>

该实现的结构对象如下所示:

pub struct Phi<'window> {
pub events: Events,
pub renderer: Renderer<'window>,
pub ttf_context: Sdl2TtfContext,

cached_fonts: HashMap<(&'static str, i32), ::sdl2_ttf::Font<'window>>
}

该方法尝试从 Phi 的 ttf_context 加载字体并将其加载到 hashmap 中。 Rust 编译器建议我在函数参数中为 self 添加生命周期,当我这样做时,会产生级联效应,为调用原始方法的每个方法添加生命周期,一直到 main() 并没有任何帮助。

由于我对 Rust 还是个新手,所以我不确定生命周期冲突在哪里或为什么会发生这种情况。作为一个猜测,我认为正在生成的 Font 对象应该在该方法结束时消失,而是被加载到 hashmap 中,其生命周期为 'window 和这两个冲突。不过,我对 Rust 的了解还不够多,无法解决这个问题,或者这是否正确。

最佳答案

这是一个重现问题的小例子:

struct FontLoader(String);
struct Font<'a>(&'a str);

impl FontLoader {
fn load(&self) -> Font {
Font(&self.0)
}
}

struct Window;

struct Phi<'window> {
window: &'window Window,
loader: FontLoader,
font: Option<Font<'window>>,
}

impl<'window> Phi<'window> {
fn do_the_thing(&mut self) {
let font = self.loader.load();
self.font = Some(font);
}
}

fn main() {}
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/main.rs:20:32
|
20 | let font = self.loader.load();
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 19:5...
--> src/main.rs:19:5
|
19 | fn do_the_thing(&mut self) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:20:20
|
20 | let font = self.loader.load();
| ^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime `'window` as defined on the impl at 18:6...
--> src/main.rs:18:6
|
18 | impl<'window> Phi<'window> {
| ^^^^^^^
note: ...so that the expression is assignable
--> src/main.rs:21:21
|
21 | self.font = Some(font);
| ^^^^^^^^^^
= note: expected `Option<Font<'window>>`
found `Option<Font<'_>>`

问题确实是你构建了一个不可能的案例。具体来说,代码说明了以下几点:

  1. Phi 将包含对 Window 的引用。该引用值在整个生命周期内都存在 'window

  2. Phi 将包含一个 Font,其中包含一个引用。该引用值在整个生命周期内都存在 'window

  3. FontLoader 返回一个 Font,其中包含对具有加载程序 生命周期的值的引用。这是由于生命周期推断,展开后如下所示:

    impl FontLoader {
    fn load<'a>(&'a self) -> Font<'a> {
    Font(&self.0)
    }
    }

    我强烈建议将 #![deny(rust_2018_idioms)] 添加到你的 crate 中,这将禁止这种特定类型的生命周期推断。

然后代码尝试从 Phi 中的 FontLoader 加载一个 Font没有 lifetime 'window 并将 Font 存储到 Phi 中。 FontLoader(因此 Font)生命周期不够长,因此无法存储在 Phi 中。

编译器正确地防止了不正确的代码。


您的下一次尝试可能是引入第二生命:

struct Phi<'window, 'font> {
window: &'window Window,
loader: FontLoader,
font: Option<Font<'font>>,
}

impl<'window, 'font> Phi<'window, 'font> {
fn do_the_thing(&'font mut self) {
let font = self.loader.load();
self.font = Some(font);
}
}

这实际上会编译,但可能不会执行您想要的操作。参见 Why can't I store a value and a reference to that value in the same struct?了解更多信息。

更有可能的是,您想引用字体加载器:

struct Phi<'a> {
window: &'a Window,
loader: &'a FontLoader,
font: Option<Font<'a>>,
}

impl<'a> Phi<'a> {
fn do_the_thing(&mut self) {
let font = self.loader.load();
self.font = Some(font);
}
}

在这里,我重命名了生命周期,因为它不再严格用于窗口。

关于rust - 由于需求冲突,无法推断出 autoref 的适当生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com