cairo/
rectangle.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4use std::fmt;
5#[cfg(feature = "use_glib")]
6use std::{marker::PhantomData, mem};
7
8#[cfg(feature = "use_glib")]
9use glib::translate::*;
10
11#[derive(Clone, Copy, PartialEq)]
12#[repr(transparent)]
13#[doc(alias = "cairo_rectangle_t")]
14pub struct Rectangle(ffi::cairo_rectangle_t);
15
16impl Rectangle {
17    #[inline]
18    pub fn new(x: f64, y: f64, width: f64, height: f64) -> Self {
19        Self(ffi::cairo_rectangle_t {
20            x,
21            y,
22            width,
23            height,
24        })
25    }
26    #[inline]
27    pub fn x(&self) -> f64 {
28        self.0.x
29    }
30    #[inline]
31    pub fn set_x(&mut self, x: f64) {
32        self.0.x = x;
33    }
34    #[inline]
35    pub fn y(&self) -> f64 {
36        self.0.y
37    }
38    #[inline]
39    pub fn set_y(&mut self, y: f64) {
40        self.0.y = y;
41    }
42    #[inline]
43    pub fn width(&self) -> f64 {
44        self.0.width
45    }
46    #[inline]
47    pub fn set_width(&mut self, width: f64) {
48        self.0.width = width;
49    }
50    #[inline]
51    pub fn height(&self) -> f64 {
52        self.0.height
53    }
54    #[inline]
55    pub fn set_height(&mut self, height: f64) {
56        self.0.height = height;
57    }
58}
59
60impl fmt::Debug for Rectangle {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        f.debug_struct("Rectangle")
63            .field("x", &self.x())
64            .field("y", &self.y())
65            .field("width", &self.width())
66            .field("height", &self.height())
67            .finish()
68    }
69}
70
71#[cfg(feature = "use_glib")]
72#[doc(hidden)]
73impl Uninitialized for Rectangle {
74    #[inline]
75    unsafe fn uninitialized() -> Self {
76        mem::zeroed()
77    }
78}
79
80#[cfg(feature = "use_glib")]
81#[doc(hidden)]
82impl<'a> ToGlibPtr<'a, *const ffi::cairo_rectangle_t> for Rectangle {
83    type Storage = PhantomData<&'a Self>;
84
85    #[inline]
86    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::cairo_rectangle_t, Self> {
87        Stash(
88            self as *const Rectangle as *const ffi::cairo_rectangle_t,
89            PhantomData,
90        )
91    }
92}
93
94#[cfg(feature = "use_glib")]
95#[doc(hidden)]
96impl<'a> ToGlibPtrMut<'a, *mut ffi::cairo_rectangle_t> for Rectangle {
97    type Storage = PhantomData<&'a mut Self>;
98
99    #[inline]
100    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::cairo_rectangle_t, Self> {
101        StashMut(
102            self as *mut Rectangle as *mut ffi::cairo_rectangle_t,
103            PhantomData,
104        )
105    }
106}
107
108#[cfg(feature = "use_glib")]
109#[doc(hidden)]
110impl FromGlibPtrNone<*const ffi::cairo_rectangle_t> for Rectangle {
111    #[inline]
112    unsafe fn from_glib_none(ptr: *const ffi::cairo_rectangle_t) -> Self {
113        *(ptr as *const Rectangle)
114    }
115}
116
117#[cfg(feature = "use_glib")]
118#[doc(hidden)]
119impl FromGlibPtrBorrow<*mut ffi::cairo_rectangle_t> for Rectangle {
120    #[inline]
121    unsafe fn from_glib_borrow(ptr: *mut ffi::cairo_rectangle_t) -> crate::Borrowed<Self> {
122        crate::Borrowed::new(*(ptr as *mut Rectangle))
123    }
124}
125
126#[cfg(feature = "use_glib")]
127#[doc(hidden)]
128impl FromGlibPtrNone<*mut ffi::cairo_rectangle_t> for Rectangle {
129    #[inline]
130    unsafe fn from_glib_none(ptr: *mut ffi::cairo_rectangle_t) -> Self {
131        *(ptr as *mut Rectangle)
132    }
133}
134
135#[cfg(feature = "use_glib")]
136gvalue_impl_inline!(
137    Rectangle,
138    ffi::cairo_rectangle_t,
139    ffi::gobject::cairo_gobject_rectangle_get_type
140);
141
142impl Rectangle {
143    #[inline]
144    pub fn to_raw_none(&self) -> *mut ffi::cairo_rectangle_t {
145        &self.0 as *const ffi::cairo_rectangle_t as *mut ffi::cairo_rectangle_t
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[cfg(feature = "use_glib")]
154    #[test]
155    fn rectangle_gvalues() {
156        use glib::prelude::*;
157
158        let rect = Rectangle::new(1., 2., 3., 4.);
159        let value = rect.to_value();
160        assert_eq!(value.get::<Rectangle>().unwrap().width(), 3.);
161        let _ = rect.to_value();
162        let rect = Some(rect);
163        let value = rect.to_value();
164        assert_eq!(
165            value.get::<Option<Rectangle>>().unwrap().map(|s| s.width()),
166            Some(3.)
167        );
168        let _ = rect.as_ref().to_value();
169        assert_eq!(
170            value
171                .get::<Option<&Rectangle>>()
172                .unwrap()
173                .map(|s| s.width()),
174            Some(3.)
175        );
176    }
177}