Hey everyone,
Trying to use rust-sdl2.
I am trying to structure my code but I am having an issue. I create a ttf_context but I can't pass it around to create fonts.
Here is the code:
extern crate sdl2;
use sdl2::ttf;
static FONT_FILE: &'static str = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf";
struct Writer<'a> {
font: ttf::Font<'a>
}
impl<'a> Writer<'a> {
fn new(ttf_context: &ttf::Sdl2TtfContext) -> Writer {
Writer {
font: ttf_context.load_font(FONT_FILE, 24).unwrap(),
}
}
}
struct GfxSubsystem<'a> {
sdl_context: sdl2::Sdl,
writer: Writer<'a>,
ttf_context: ttf::Sdl2TtfContext,
}
impl<'a> GfxSubsystem<'a> {
fn new() -> GfxSubsystem<'a> {
let sdl_context = sdl2::init().unwrap();
let ttf_context = sdl2::ttf::init().unwrap();
let writer = Writer::new(&ttf_context);
^^^^^^^^^^^ does not live long enough
GfxSubsystem {
sdl_context: sdl_context,
writer: writer,
ttf_context: ttf_context,
}
}
}
fn main() {
let gss = GfxSubsystem::new();
}
Why ttf_context does not live long enough? It is saved in the GfxSubsystem struct which itself owns a Writer struct so there should be no problem. And anyway, I only use temporarily in Writer to create a Font which lifetime is linked to the Writer struct lifetime.
Any help is appreciated...
Luke