Skip to content

Adds from_static_bytes function to Chunk struct #922

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/sdl2/mixer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,27 @@ impl Chunk {
}
}

/// Load chunk from a static byte buffer.
pub fn from_static_bytes(buf: &'static [u8]) -> Result<Chunk, String> {
let rw = unsafe {
sys::SDL_RWFromConstMem(buf.as_ptr() as *const c_void, buf.len() as c_int)
};
Comment on lines +255 to +257

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you could use RWops there (exactly RWops::from_bytes(buf)), I've seen other methods to do this and even make it a one-liner in raw declaration (avoiding declaring another var and doing another check I guess). I'm not sure about the benefits with that other than avoiding the use of another unsafe block (I'm not the Rust expert, so I might still be unfamiliar with that language design and quirks, same goes with SDL2).


if rw.is_null() {
return Err(get_error());
}

let raw = unsafe { mixer::Mix_LoadWAV_RW(rw, 0) };
if raw.is_null() {
Err(get_error())
} else {
Ok(Chunk {
raw: raw,
owned: true,
})
}
Comment on lines +264 to +271

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw in different from_* methods that there was a private method to just do this stuff to avoid duplicates in the code. Any reason it isn't used there?

EDIT: I guess the code base is out-of-date just by looking at the extended diff… or at least different from the latest stable release. I guess this is also one of the things that should be checked after rebasing the fork and before merging it.

}

/// Set chunk->volume to volume.
pub fn set_volume(&mut self, volume: i32) -> i32 {
unsafe { mixer::Mix_VolumeChunk(self.raw, volume as c_int) as i32 }
Expand Down