use rectf::Rectf;
use recti::Recti;
#[cfg(all(windows, feature = "d2d"))]
use winapi::um::dcommon::D2D_RECT_U;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Rectu {
pub left: u32,
pub top: u32,
pub right: u32,
pub bottom: u32,
}
impl Rectu {
#[inline]
pub fn new(left: u32, top: u32, right: u32, bottom: u32) -> Rectu {
Rectu {
left,
top,
right,
bottom,
}
}
#[inline]
pub fn to_f32(&self) -> Rectf {
Rectf {
left: self.left as f32,
top: self.top as f32,
right: self.right as f32,
bottom: self.bottom as f32,
}
}
#[inline]
pub fn to_i32(&self) -> Recti {
Recti {
left: self.left as i32,
top: self.top as i32,
right: self.right as i32,
bottom: self.bottom as i32,
}
}
}
#[cfg(all(windows, feature = "d2d"))]
impl From<Rectu> for D2D_RECT_U {
#[inline]
fn from(rect: Rectu) -> D2D_RECT_U {
D2D_RECT_U {
left: rect.left,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
}
}
}
#[cfg(all(windows, feature = "d2d"))]
impl From<D2D_RECT_U> for Rectu {
#[inline]
fn from(rect: D2D_RECT_U) -> Rectu {
Rectu {
left: rect.left,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
}
}
}
#[cfg(all(test, windows, feature = "d2d"))]
#[test]
fn rectu_d2d_bin_compat() {
use std::mem::size_of_val;
fn ptr_eq<T>(a: &T, b: &T) -> bool {
(a as *const T) == (b as *const T)
}
let rect = Rectu::new(0, 0, 0, 0);
let d2d = unsafe { &*((&rect) as *const _ as *const D2D_RECT_U) };
assert!(ptr_eq(&rect.left, &d2d.left));
assert!(ptr_eq(&rect.top, &d2d.top));
assert!(ptr_eq(&rect.right, &d2d.right));
assert!(ptr_eq(&rect.bottom, &d2d.bottom));
assert_eq!(size_of_val(&rect), size_of_val(d2d));
}