i'm trying to wrap sdl2 in an abstraction layer, but the borrow checker is giving me trouble.
use sdl2::Sdl;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::ttf::{Sdl2TtfContext, Font};
// apperently NixOS doesn't have fonts in any of the places you would expect.
static FONT: &[u8] = include_bytes!("SomeFont.ttf");
pub struct SdlWindow<'a> {
sdl: Sdl,
ttf: Sdl2TtfContext,
font: Option<Font<'a, 'a>>,
}
impl SdlWindow<'_> {
fn new() -> Box<Self> {
let fontrw = sdl2::rwops::RWops::from_bytes(FONT).unwrap();
let sdl = sdl2::init().unwrap();
let ttf = sdl2::ttf::init().unwrap();
let mut w = Box::new(SdlWindow{
sdl,
ttf,
font: None,
});
w.font = Some(w.ttf.load_font_from_rwops(fontrw, 16).unwrap());
w
}
}
my only idea currently is to make the Sdl2TtfContext have a static lifetime, but that means its destructor will never be run, possibly leaking persistent resources like tempfiles or X server PixBufs, so i would like to avoid that. maybe something could be done involving Pin, but i'm not sure.