rust-sdl2 ttf字体和生命周期

时间:2017-01-07 22:18:28

标签: rust

我跟随此tutorial,但尝试更新它以使用最新版本的rust-sdl2,其中已包含sdl2_ttf和sdl2_image的绑定。我的问题在于以下代码

pub struct Phi<'window> {
    pub events: Events,
    pub renderer: ::sdl2::render::Renderer<'window>,
    ttf_context: ::sdl2::ttf::Sdl2TtfContext,
    cached_fonts: HashMap<(&'static str, u16), ::sdl2::ttf::Font<'window>>,
}

impl<'window> Phi<'window> {
    fn new (events: Events, renderer: ::sdl2::render::Renderer<'window>, 
        ttf_context: ::sdl2::ttf::Sdl2TtfContext) -> Phi<'window> {

        Phi {
            events: events,
            renderer: renderer,
            ttf_context: ttf_context,
            cached_fonts: HashMap::new(),
        }
    }

    pub fn output_size(&self) -> (f64,f64) {
        let (w,h) = self.renderer.output_size().unwrap();
        (w as f64, h as f64)
    }

    pub fn ttf_str_sprite (&mut self, text: &str, font_path: &'static str, 
        size: u16, 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)
        }
        self.ttf_context.load_font(Path::new(font_path), size).ok()
            .and_then(|font| {
                self.cached_fonts.insert((font_path, size), font);
                self.ttf_str_sprite(text, font_path, size, color)
            })
    }
}

我收到此编译错误

Compiling arcade-rs v0.1.0 (file:///D:/Programming/Rust/arcade-rs)
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src\phi/mod.rs:58:20
   |
58 |            self.ttf_context.load_font(Path::new(font_path), size).ok()
   |                             ^^^^^^^^^
   |
help: consider using an explicit lifetime parameter as shown: fn ttf_str_sprite(&'window mut self, text: &str, font_path: &'static str,
                  size: u16, color: Color) -> Option<Sprite>
  --> src\phi/mod.rs:50:2
   |
50 |    pub fn ttf_str_sprite (&mut self, text: &str, font_path: &'static str,
   |    ^

error: aborting due to previous error

error: Could not compile `arcade-rs`.

如果我同意编译器建议,原始代码现在编译得很好,但是

struct Action{

    func: Box<Fn(&mut Phi) -> ViewAction>,
    idle_sprite: Sprite,
    hover_sprite: Sprite,
}

impl Action{

    fn new(phi: &mut Phi, label: &'static str, func: Box< Fn(&mut Phi) -> ViewAction>)
        -> Action{
            Action {
                func: func,
                idle_sprite: phi.ttf_str_sprite(label, "assets/belligerent.ttf", 32, 
                    Color::RGB(220,220,220)).unwrap(),
                hover_sprite: phi.ttf_str_sprite(label, "assets/belligerent.ttf", 38,
                    Color::RGB(255,255,255)).unwrap(),
            }
        }
}

现在给出了编译错误

   Compiling arcade-rs v0.1.0 (file:///D:/Programming/Rust/arcade-rs)
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src\views\main_menu.rs:95:22
   |
95 |                            idle_sprite: phi.ttf_str_sprite(label, "assets/belligerent.ttf", 32,
   |                                             ^^^^^^^^^^^^^^

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src\views\main_menu.rs:97:23
   |
97 |                            hover_sprite: phi.ttf_str_sprite(label, "assets/belligerent.ttf", 38,
   |                                              ^^^^^^^^^^^^^^
   |
help: consider using an explicit lifetime parameter as shown: fn new<'a>(phi: &'a mut Phi<'a>, label: &'static str,
           func: Box<Fn(&mut Phi) -> ViewAction>) -> Action
  --> src\views\main_menu.rs:91:2
   |
91 |    fn new(phi: &mut Phi, label: &'static str, func: Box< Fn(&mut Phi) -> ViewAction>)
   |    ^

error: aborting due to 2 previous errors
error: Could not compile `arcade-rs`.

遵循编译器建议一直导致死胡同。我不确定为什么它甚至会关心这种情况下的生命周期,因为不仅仅是Action所拥有的Sprites?

0 个答案:

没有答案