From f81bc3c95a0ee388fe91f1e97cb2efd0cc186840 Mon Sep 17 00:00:00 2001 From: Edhebi Date: Sat, 8 Jun 2019 17:21:01 +0200 Subject: [PATCH 1/2] added conversion from PixelFormatEnum to PixelFormat --- src/sdl2/pixels.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sdl2/pixels.rs b/src/sdl2/pixels.rs index cc72869f6ab..be1c3b3417f 100644 --- a/src/sdl2/pixels.rs +++ b/src/sdl2/pixels.rs @@ -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; @@ -460,6 +461,21 @@ impl FromPrimitive for PixelFormatEnum { fn from_u64(n: u64) -> Option { FromPrimitive::from_i64(n as i64) } } +impl TryFrom for PixelFormat { + type Error = String; + + fn try_from(pfe: PixelFormatEnum) -> Result { + 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. From 1db3a05e8342dfe46e2d2950e316abff3c69ecf8 Mon Sep 17 00:00:00 2001 From: Edhebi Date: Sat, 8 Jun 2019 17:36:42 +0200 Subject: [PATCH 2/2] add changelog message --- changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/changelog.md b/changelog.md index f042382e09f..0d7fd24f588 100644 --- a/changelog.md +++ b/changelog.md @@ -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` for `PixelFormat` + ### v0.32.1 [PR #868](https://github.com/Rust-SDL2/rust-sdl2/pull/868):