Skip to content

Commit

Permalink
Merge pull request Rust-SDL2#898 from Edhebi/pixelformat-from-pixelfo…
Browse files Browse the repository at this point in the history
…rmatenum

Conversion from PixelFormatEnum to PixelFormat
  • Loading branch information
Cobrand authored Jun 8, 2019
2 parents ad02cc0 + 2113b8e commit a02ecfe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit a02ecfe

Please sign in to comment.