Skip to content
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

Conversion from PixelFormatEnum to PixelFormat #898

Merged
merged 2 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
In this file will be listed the changes, especially the breaking ones that one should be careful of
when upgrading from a version of rust-sdl2 to another.

### v0.32.2

[PR #898](https://github.com/Rust-SDL2/rust-sdl2/pull/898):
Implements `TryFrom<PixelFormatEnum>` for `PixelFormat`

### v0.32.1

[PR #868](https://github.com/Rust-SDL2/rust-sdl2/pull/868):
Expand Down
16 changes: 16 additions & 0 deletions src/sdl2/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use self::rand::distributions::{Distribution, Standard};
use libc::uint32_t;
use num::FromPrimitive;
use std::mem::transmute;
use std::convert::TryFrom;
use crate::sys;

use crate::get_error;
Expand Down Expand Up @@ -460,6 +461,21 @@ impl FromPrimitive for PixelFormatEnum {
fn from_u64(n: u64) -> Option<PixelFormatEnum> { FromPrimitive::from_i64(n as i64) }
}

impl TryFrom<PixelFormatEnum> for PixelFormat {
type Error = String;

fn try_from(pfe: PixelFormatEnum) -> Result<Self, Self::Error> {
unsafe {
let pf_ptr = sys::SDL_AllocFormat(pfe as u32);
if pf_ptr.is_null() {
Err(get_error())
} else {
Ok(PixelFormat::from_ll(pf_ptr))
}
}
}
}


// Just test a round-trip conversion from PixelFormat to
// PixelFormatEnum and back.
Expand Down