Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sdl2TtfContext should need lifetime #1369

Closed
wants to merge 5 commits into from

Conversation

ZengGengSen
Copy link
Contributor

pub struct FontManager<'ttf_module> {
    ttf_ctx: sdl2::ttf::Sdl2TtfContext,
    fonts: HashMap<String, sdl2::ttf::Font<'ttf_module, 'static>>,
}

impl<'ttf_module> FontManager<'ttf_module> {
    pub fn new() -> Result<Self, Error> {
        let ttf_ctx = sdl2::ttf::init()?;

        Ok(Self {
            ttf_ctx,
            fonts: HashMap::new(),
        })
    }

    pub fn load_font<P: AsRef<std::path::Path>>(
        &mut self,
        path: P,
        name: &str,
        point_size: u16,
    ) -> Result<&mut sdl2::ttf::Font<'ttf_module, 'static>, Error> {
        let font = self.ttf_ctx.load_font(path, point_size);
        // lifetime error
        self.fonts.insert(name.to_string(), font?);
        Ok(self.fonts.get_mut(name).unwrap())
    }
}

    The lifetime of load_font is different from that of ctx
    &Path not need clone
    use of deprecated method `std::error::Error::description`:
    use the Display impl or to_string()
    fix build example error(examples/resource-manager.rs)
@ZengGengSen ZengGengSen closed this Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant