graphene/
rect.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Rect, Vec2};
8
9impl Rect {
10    #[doc(alias = "graphene_rect_get_vertices")]
11    #[doc(alias = "get_vertices")]
12    pub fn vertices(&self) -> &[Vec2; 4] {
13        unsafe {
14            let mut out: [ffi::graphene_vec2_t; 4] = std::mem::zeroed();
15            ffi::graphene_rect_get_vertices(self.to_glib_none().0, &mut out as *mut _);
16            &*(&out as *const [ffi::graphene_vec2_t; 4] as *const [Vec2; 4])
17        }
18    }
19
20    #[doc(alias = "graphene_rect_init")]
21    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
22        assert_initialized_main_thread!();
23        unsafe {
24            let mut rect = Self::uninitialized();
25            ffi::graphene_rect_init(rect.to_glib_none_mut().0, x, y, width, height);
26            rect
27        }
28    }
29}
30
31impl fmt::Debug for Rect {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        f.debug_struct("Rect")
34            .field("x", &self.x())
35            .field("y", &self.y())
36            .field("width", &self.width())
37            .field("height", &self.height())
38            .finish()
39    }
40}
41
42impl Default for Rect {
43    fn default() -> Self {
44        Self::zero()
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use crate::Point;
52
53    #[test]
54    fn contains_point() {
55        let rect = Rect::new(100., 100., 100., 100.);
56
57        let right = Point::new(250., 150.);
58        let below = Point::new(150., 50.);
59        let left = Point::new(50., 150.);
60        let above = Point::new(150., 250.);
61
62        assert!(!rect.contains_point(&right));
63        assert!(!rect.contains_point(&below));
64        assert!(!rect.contains_point(&left));
65        assert!(!rect.contains_point(&above));
66    }
67}