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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::{cvt, get_optional, Alignment, Geometry};
use std::io;
use std::marker::PhantomData;
use libparted_sys::{
ped_constraint_destroy, ped_constraint_done, ped_constraint_duplicate, ped_constraint_init,
ped_constraint_intersect, ped_constraint_is_solution, ped_constraint_new,
ped_constraint_new_from_max, ped_constraint_new_from_min, ped_constraint_new_from_min_max,
ped_constraint_solve_max, ped_constraint_solve_nearest, PedConstraint,
};
pub(crate) enum ConstraintSource {
Init,
New,
}
pub struct Constraint<'a> {
pub(crate) constraint: *mut PedConstraint,
pub(crate) source: ConstraintSource,
pub(crate) phantom: PhantomData<&'a PedConstraint>,
}
impl<'a> Constraint<'a> {
fn new_(constraint: *mut PedConstraint, source: ConstraintSource) -> Constraint<'a> {
Constraint {
constraint,
source,
phantom: PhantomData,
}
}
pub fn new(
start_align: &Alignment,
end_align: &Alignment,
start_range: &Geometry,
end_range: &Geometry,
min_size: i64,
max_size: i64,
) -> io::Result<Constraint<'a>> {
cvt(unsafe {
ped_constraint_new(
start_align.alignment,
end_align.alignment,
start_range.geometry,
end_range.geometry,
min_size,
max_size,
)
})
.map(|constraint| Constraint::new_(constraint, ConstraintSource::New))
}
pub fn new_from_max(max: &Geometry) -> io::Result<Constraint<'a>> {
cvt(unsafe { ped_constraint_new_from_max(max.geometry) })
.map(|constraint| Constraint::new_(constraint, ConstraintSource::New))
}
pub fn new_from_min(min: &Geometry) -> io::Result<Constraint<'a>> {
cvt(unsafe { ped_constraint_new_from_min(min.geometry) })
.map(|constraint| Constraint::new_(constraint, ConstraintSource::New))
}
pub fn new_from_min_max(min: &Geometry, max: &Geometry) -> io::Result<Constraint<'a>> {
cvt(unsafe { ped_constraint_new_from_min_max(min.geometry, max.geometry) })
.map(|constraint| Constraint::new_(constraint, ConstraintSource::New))
}
pub fn init(
&mut self,
start_align: &Alignment,
end_align: &Alignment,
start_range: &Geometry,
end_range: &Geometry,
min_size: i64,
max_size: i64,
) -> io::Result<()> {
cvt(unsafe {
ped_constraint_init(
self.constraint,
start_align.alignment,
end_align.alignment,
start_range.geometry,
end_range.geometry,
min_size,
max_size,
)
})?;
self.source = ConstraintSource::Init;
Ok(())
}
pub fn start_align(&self) -> Alignment {
Alignment::from_raw(unsafe { (*self.constraint).start_align })
}
pub fn end_align(&self) -> Alignment {
Alignment::from_raw(unsafe { (*self.constraint).end_align })
}
pub fn start_range(&self) -> Geometry {
Geometry::from_raw(unsafe { (*self.constraint).start_range })
}
pub fn end_range(&self) -> Geometry {
Geometry::from_raw(unsafe { (*self.constraint).end_range })
}
pub fn min_size(&'a self) -> i64 {
unsafe { (*self.constraint).min_size }
}
pub fn max_size(&'a self) -> i64 {
unsafe { (*self.constraint).max_size }
}
pub fn duplicate<'b>(&self) -> io::Result<Constraint<'b>> {
cvt(unsafe { ped_constraint_duplicate(self.constraint) })
.map(|constraint| Constraint::new_(constraint, ConstraintSource::New))
}
pub fn intersect(&self, other: &Constraint) -> Option<Constraint<'a>> {
get_optional(unsafe { ped_constraint_intersect(self.constraint, other.constraint) })
.map(|constraint| Constraint::new_(constraint, ConstraintSource::New))
}
pub fn is_solution(&self, geometry: &Geometry) -> bool {
unsafe { ped_constraint_is_solution(self.constraint, geometry.geometry) == 1 }
}
pub fn solve_max(&self) -> Option<Geometry<'a>> {
get_optional(unsafe { ped_constraint_solve_max(self.constraint) }).map(Geometry::from_raw)
}
pub fn solve_nearest(&self, geom: &Geometry) -> Option<Geometry<'a>> {
get_optional(unsafe { ped_constraint_solve_nearest(self.constraint, geom.geometry) })
.map(Geometry::from_raw)
}
}
impl<'a> Drop for Constraint<'a> {
fn drop(&mut self) {
unsafe {
match self.source {
ConstraintSource::Init => ped_constraint_done(self.constraint),
ConstraintSource::New => ped_constraint_destroy(self.constraint),
}
}
}
}