Skip to content

Commit b52bd54

Browse files
FlaiseCobrand
authored andcommitted
Add Music::from_bytes() function (#704)
1 parent 6bea457 commit b52bd54

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ breaking exisitng code too much.
1818
* Adds the `unsafe_textures` feature to this crate, allowing to get rid of the lifetimes
1919
in `Texture`s in the `render` module.
2020

21+
[PR #704](https://github.com/Rust-SDL2/rust-sdl2/pull/704)
22+
23+
* Adds the `Music::from_static_bytes` function, which creates a Music instance with the
24+
static lifetime from a buffer that also has a static lifetime.
25+
2126
### v0.30
2227

2328
Re-exported sdl2\_sys as sdl2::sys

src/sdl2/mixer/mod.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//!
22
//! A binding for the library `SDL2_mixer`
33
//!
4-
//!
4+
//!
55
//! Note that you need to build with the
66
//! feature `mixer` for this module to be enabled,
77
//! like so:
@@ -27,7 +27,7 @@ use std::ffi::{CString, CStr};
2727
use std::str::from_utf8;
2828
use std::borrow::ToOwned;
2929
use std::path::Path;
30-
use libc::{c_int, uint16_t, c_double, c_uint};
30+
use libc::{c_int, uint16_t, c_double, c_uint, c_void};
3131
use ::get_error;
3232
use ::rwops::RWops;
3333
use ::version::Version;
@@ -790,6 +790,28 @@ impl<'a> Music<'a> {
790790
}
791791
}
792792

793+
/// Load music from a static byte buffer.
794+
pub fn from_static_bytes(buf: &'static [u8]) -> Result<Music<'static>, String> {
795+
let rw = unsafe {
796+
::sys::rwops::SDL_RWFromConstMem(buf.as_ptr() as *const c_void, buf.len() as c_int)
797+
};
798+
799+
if rw.is_null() {
800+
return Err(get_error());
801+
}
802+
803+
let raw = unsafe { ffi::Mix_LoadMUS_RW(rw, 0) };
804+
if raw.is_null() {
805+
Err(get_error())
806+
} else {
807+
Ok(Music {
808+
raw: raw,
809+
owned: true,
810+
_marker: PhantomData,
811+
})
812+
}
813+
}
814+
793815
/// The file format encoding of the music.
794816
pub fn get_type(&self) -> MusicType {
795817
let ret = unsafe { ffi::Mix_GetMusicType(self.raw) as i32 } as c_uint;

0 commit comments

Comments
 (0)