1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Take a look at the license at the top of the repository in the LICENSE file.

use std::fmt;

use cairo::RectangleInt;
use glib::translate::*;

use crate::Rectangle;

impl Rectangle {
    #[inline]
    pub fn new(x: i32, y: i32, width: i32, height: i32) -> Self {
        skip_assert_initialized!();
        unsafe {
            Self::unsafe_from(ffi::GdkRectangle {
                x,
                y,
                width,
                height,
            })
        }
    }

    #[inline]
    pub fn x(&self) -> i32 {
        self.inner.x
    }

    #[inline]
    pub fn set_x(&mut self, x: i32) {
        self.inner.x = x;
    }

    #[inline]
    pub fn y(&self) -> i32 {
        self.inner.y
    }

    #[inline]
    pub fn set_y(&mut self, y: i32) {
        self.inner.y = y;
    }

    #[inline]
    pub fn width(&self) -> i32 {
        self.inner.width
    }

    #[inline]
    pub fn set_width(&mut self, width: i32) {
        self.inner.width = width;
    }

    #[inline]
    pub fn height(&self) -> i32 {
        self.inner.height
    }

    #[inline]
    pub fn set_height(&mut self, height: i32) {
        self.inner.height = height;
    }
}

impl fmt::Debug for Rectangle {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Rectangle")
            .field("x", &self.x())
            .field("y", &self.y())
            .field("width", &self.width())
            .field("height", &self.height())
            .finish()
    }
}

impl AsRef<RectangleInt> for Rectangle {
    #[inline]
    fn as_ref(&self) -> &RectangleInt {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl From<RectangleInt> for Rectangle {
    #[inline]
    fn from(r: RectangleInt) -> Self {
        skip_assert_initialized!();
        unsafe { *(&r as *const _ as *const _) }
    }
}

impl From<Rectangle> for RectangleInt {
    #[inline]
    fn from(r: Rectangle) -> Self {
        skip_assert_initialized!();
        unsafe { *(&r as *const _ as *const _) }
    }
}

impl From<cairo::Rectangle> for Rectangle {
    // rustdoc-stripper-ignore-next
    /// Note that converting between a [`cairo::Rectangle`] and [`Rectangle`]
    /// will probably lead to precisison errors. Use cautiously.
    #[inline]
    fn from(r: cairo::Rectangle) -> Self {
        skip_assert_initialized!();
        Self::new(
            r.x() as i32,
            r.y() as i32,
            r.width() as i32,
            r.height() as i32,
        )
    }
}

impl From<Rectangle> for cairo::Rectangle {
    #[inline]
    fn from(r: Rectangle) -> Self {
        skip_assert_initialized!();
        cairo::Rectangle::new(
            r.x() as f64,
            r.y() as f64,
            r.width() as f64,
            r.height() as f64,
        )
    }
}

impl From<pango::Rectangle> for Rectangle {
    #[inline]
    fn from(r: pango::Rectangle) -> Self {
        skip_assert_initialized!();
        Self::new(r.x(), r.y(), r.width(), r.height())
    }
}

impl From<Rectangle> for pango::Rectangle {
    #[inline]
    fn from(r: Rectangle) -> Self {
        skip_assert_initialized!();
        Self::new(r.x(), r.y(), r.width(), r.height())
    }
}