RicoA
RicoA

Reputation: 31

Is there a possibility to create a const value with non const functions?

In my code I want to load the raw data of a font installed on my Linux. For this I took the crate font-loader. Up this point there is no issue. But this crate gives me the raw data as Vec<u8>.

This font should then be used by the graphics library embedded-graphics, where I took u8g2-fonts as a way to handle different fonts.

But for this to work I have to implement Font for a custom struct:

pub struct libertation_font;
impl Font for libertation_font {
    const DATA: &'static [u8] = todo!();
}

Coming to my question. Is it possible to load the font with font-loader and implement the Font trait where the data is what I got from font-loader? Or is this impossible due to the way Rust handles const?

Upvotes: 3

Views: 679

Answers (1)

Aleksander Krauze
Aleksander Krauze

Reputation: 6120

No. const values must be evaluateable at compile time. That means they must be initialized by a const function. You could also try using include_bytes! macro and/or writing a build script that at compile time will load fonts and include them in the binary. But you cannot do that at runtime.

Upvotes: 2

Related Questions