File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ fn main() {
34
34
Style :: CLOSE ,
35
35
& ContextSettings :: default ( ) ,
36
36
) ;
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 ( ) ;
38
38
39
39
while rw. is_open ( ) {
40
40
while let Some ( ev) = rw. poll_event ( ) {
Original file line number Diff line number Diff line change @@ -114,13 +114,24 @@ impl Font {
114
114
/// SFML cannot preload all the font data in this function, so the buffer pointed by `memory`
115
115
/// has to remain valid until the `Font` object loads a new font or is destroyed.
116
116
///
117
- /// See also
117
+ /// For a safe version, see [`Font::from_memory_static`].
118
+ ///
119
+ /// # See also
120
+ ///
118
121
/// [`Font::from_file`], [`Font::from_stream`]
119
122
pub unsafe fn from_memory ( memory : & [ u8 ] ) -> SfResult < SfBox < Self > > {
120
123
let fnt =
121
124
unsafe { ffi:: sfFont_createFromMemory ( memory. as_ptr ( ) as * const _ , memory. len ( ) ) } ;
122
125
SfBox :: new ( fnt) . into_sf_result ( )
123
126
}
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
+ }
124
135
}
125
136
126
137
/// Font information, properties, glyph fetch
Original file line number Diff line number Diff line change @@ -255,14 +255,26 @@ impl RcFont {
255
255
/// SFML cannot preload all the font data in this function, so the buffer pointed by `memory`
256
256
/// has to remain valid until the `RcFont` object loads a new font or is destroyed.
257
257
///
258
- /// See also
258
+ /// For a safe version, see [`RcFont::from_memory_static`].
259
+ ///
260
+ /// # See also
261
+ ///
259
262
/// [`RcFont::from_file`], [`RcFont::from_stream`]
260
263
pub unsafe fn from_memory ( memory : & [ u8 ] ) -> SfResult < Self > {
261
264
Ok ( RcFont {
262
265
font : Rc :: new ( RefCell :: new ( unsafe { Font :: from_memory ( memory) } ?) ) ,
263
266
} )
264
267
}
265
268
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
+
266
278
/// Get the texture containing the glyphs of a given size in a font
267
279
///
268
280
/// # Arguments
You can’t perform that action at this time.
0 commit comments