Skip to content

Commit fc390c8

Browse files
Add safe variant of Font::from_memory, that has 'static requirement
1 parent a916046 commit fc390c8

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

examples/window-test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn main() {
3434
Style::CLOSE,
3535
&ContextSettings::default(),
3636
);
37-
let font = unsafe { Font::from_memory(include_bytes!("resources/sansation.ttf")) }.unwrap();
37+
let font = Font::from_memory_static(include_bytes!("resources/sansation.ttf")).unwrap();
3838

3939
while rw.is_open() {
4040
while let Some(ev) = rw.poll_event() {

src/graphics/font.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,24 @@ impl Font {
114114
/// SFML cannot preload all the font data in this function, so the buffer pointed by `memory`
115115
/// has to remain valid until the `Font` object loads a new font or is destroyed.
116116
///
117-
/// See also
117+
/// For a safe version, see [`Font::from_memory_static`].
118+
///
119+
/// # See also
120+
///
118121
/// [`Font::from_file`], [`Font::from_stream`]
119122
pub unsafe fn from_memory(memory: &[u8]) -> SfResult<SfBox<Self>> {
120123
let fnt =
121124
unsafe { ffi::sfFont_createFromMemory(memory.as_ptr() as *const _, memory.len()) };
122125
SfBox::new(fnt).into_sf_result()
123126
}
127+
/// Load the font from a file in static memory.
128+
///
129+
/// This function is safe because the font will stay in memory as long as required.
130+
///
131+
/// See [`Font::from_memory`]
132+
pub fn from_memory_static(memory: &'static [u8]) -> SfResult<SfBox<Self>> {
133+
unsafe { Self::from_memory(memory) }
134+
}
124135
}
125136

126137
/// Font information, properties, glyph fetch

src/graphics/rc_font.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,26 @@ impl RcFont {
255255
/// SFML cannot preload all the font data in this function, so the buffer pointed by `memory`
256256
/// has to remain valid until the `RcFont` object loads a new font or is destroyed.
257257
///
258-
/// See also
258+
/// For a safe version, see [`RcFont::from_memory_static`].
259+
///
260+
/// # See also
261+
///
259262
/// [`RcFont::from_file`], [`RcFont::from_stream`]
260263
pub unsafe fn from_memory(memory: &[u8]) -> SfResult<Self> {
261264
Ok(RcFont {
262265
font: Rc::new(RefCell::new(unsafe { Font::from_memory(memory) }?)),
263266
})
264267
}
265268

269+
/// Load the font from a file in static memory.
270+
///
271+
/// This function is safe because the font will stay in memory as long as required.
272+
///
273+
/// See [`RcFont::from_memory`]
274+
pub fn from_memory_static(memory: &'static [u8]) -> SfResult<Self> {
275+
unsafe { Self::from_memory(memory) }
276+
}
277+
266278
/// Get the texture containing the glyphs of a given size in a font
267279
///
268280
/// # Arguments

0 commit comments

Comments
 (0)