Skip to content

Commit c7b4b53

Browse files
committed
Drop WalkAction and WalkResult in favor of ControlFlow from std
1 parent 7cb8dcb commit c7b4b53

File tree

11 files changed

+137
-155
lines changed

11 files changed

+137
-155
lines changed

binding-generator/src/bin/settings-cleanup.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::cell::RefCell;
22
use std::collections::HashSet;
3+
use std::ops::ControlFlow;
34
use std::path::{Path, PathBuf};
45
use std::{env, fmt};
56

67
use clang::{Entity, EntityKind};
78

89
use opencv_binding_generator::{
910
opencv_module_from_path, settings, Class, EntityExt, EntityWalkerExt, EntityWalkerVisitor, Func, FuncId, Generator,
10-
GeneratorEnv, WalkAction,
11+
GeneratorEnv,
1112
};
1213

1314
struct FunctionFinder<'tu, 'f> {
@@ -41,7 +42,7 @@ impl<'tu> EntityWalkerVisitor<'tu> for FunctionFinder<'tu, '_> {
4142
opencv_module_from_path(path).map_or(false, |m| m == self.gen_env.module())
4243
}
4344

44-
fn visit_entity(&mut self, entity: Entity<'tu>) -> WalkAction {
45+
fn visit_entity(&mut self, entity: Entity<'tu>) -> ControlFlow<()> {
4546
match entity.get_kind() {
4647
EntityKind::ClassDecl
4748
| EntityKind::ClassTemplate
@@ -59,19 +60,18 @@ impl<'tu> EntityWalkerVisitor<'tu> for FunctionFinder<'tu, '_> {
5960
if func.is_generic() {
6061
self.update_used_func(&func);
6162
}
62-
WalkAction::Continue
63+
ControlFlow::Continue(())
6364
});
6465
entity.walk_classes_while(|child| self.visit_entity(child));
6566
}
66-
WalkAction::Continue
6767
}
6868
EntityKind::FunctionDecl => {
6969
let f = Func::new(entity, &self.gen_env);
7070
self.update_used_func(&f);
71-
WalkAction::Continue
7271
}
73-
_ => WalkAction::Continue,
72+
_ => {}
7473
}
74+
ControlFlow::Continue(())
7575
}
7676
}
7777

binding-generator/src/class.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::collections::HashSet;
33
use std::hash::{Hash, Hasher};
4+
use std::ops::ControlFlow;
45
use std::rc::Rc;
56
use std::{fmt, iter};
67

@@ -11,7 +12,7 @@ pub use desc::ClassDesc;
1112
use crate::comment::strip_doxygen_comment_markers;
1213
use crate::debug::{DefinitionLocation, LocationName};
1314
use crate::element::ExcludeKind;
14-
use crate::entity::{ToEntity, WalkAction, WalkResult};
15+
use crate::entity::{ControlFlowExt, ToEntity};
1516
use crate::field::FieldDesc;
1617
use crate::func::{FuncCppBody, FuncDesc, FuncKind, FuncRustBody, FuncRustExtern, ReturnKind};
1718
use crate::type_ref::{Constness, CppNameStyle, StrEnc, StrType, TypeRef, TypeRefDesc, TypeRefTypeHint};
@@ -66,9 +67,9 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
6667
&& !self
6768
.for_each_field(|field| {
6869
let type_ref = field.type_ref();
69-
WalkAction::continue_until(!type_ref.kind().is_copy(type_ref.type_hint()))
70+
ControlFlow::continue_until(!type_ref.kind().is_copy(type_ref.type_hint()))
7071
})
71-
.is_interrupted()
72+
.is_break()
7273
}
7374

7475
pub fn kind(&self) -> ClassKind {
@@ -154,8 +155,8 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
154155
pub fn is_polymorphic(&self) -> bool {
155156
match self {
156157
Self::Clang { entity, .. } => entity
157-
.walk_methods_while(|f| WalkAction::continue_until(f.is_virtual_method() || f.is_pure_virtual_method()))
158-
.is_interrupted(),
158+
.walk_methods_while(|f| ControlFlow::continue_until(f.is_virtual_method() || f.is_pure_virtual_method()))
159+
.is_break(),
159160
Self::Desc(_) => false,
160161
}
161162
}
@@ -185,9 +186,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
185186

186187
/// Class has an explicit method named `clone()`
187188
pub fn has_explicit_clone(&self) -> bool {
188-
self
189-
.for_each_method(|m| WalkAction::continue_until(m.is_clone()))
190-
.is_interrupted()
189+
self.for_each_method(|m| ControlFlow::continue_until(m.is_clone())).is_break()
191190
}
192191

193192
/// Class is simple (i.e. constructor-copiable in C++), but can't be simple in Rust
@@ -200,21 +199,21 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
200199
&Self::Clang { entity, .. } => !entity
201200
.walk_children_while(|f| {
202201
if matches!(f.get_kind(), EntityKind::Constructor) {
203-
WalkAction::Interrupt
202+
ControlFlow::Break(())
204203
} else {
205-
WalkAction::Continue
204+
ControlFlow::Continue(())
206205
}
207206
})
208-
.is_interrupted(),
207+
.is_break(),
209208
Self::Desc(_) => false,
210209
}
211210
}
212211

213212
pub fn has_virtual_destructor(&self) -> bool {
214213
match self {
215214
Class::Clang { entity, .. } => entity
216-
.walk_children_while(|f| WalkAction::continue_until(f.get_kind() == EntityKind::Destructor && f.is_virtual_method()))
217-
.is_interrupted(),
215+
.walk_children_while(|f| ControlFlow::continue_until(f.get_kind() == EntityKind::Destructor && f.is_virtual_method()))
216+
.is_break(),
218217
Class::Desc(_) => false,
219218
}
220219
}
@@ -223,19 +222,19 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
223222
match self {
224223
Class::Clang { entity, .. } => entity
225224
.walk_children_while(|f| {
226-
WalkAction::continue_until(
225+
ControlFlow::continue_until(
227226
f.get_kind() == EntityKind::Destructor
228227
&& f.get_accessibility().map_or(true, |acc| acc != Accessibility::Public),
229228
)
230229
})
231-
.is_interrupted(),
230+
.is_break(),
232231
Class::Desc(_) => false,
233232
}
234233
}
235234

236235
pub fn has_bases(&self) -> bool {
237236
match self {
238-
&Self::Clang { entity, .. } => entity.walk_bases_while(|_| WalkAction::Interrupt).is_interrupted(),
237+
&Self::Clang { entity, .. } => entity.walk_bases_while(|_| ControlFlow::Break(())).is_break(),
239238
Self::Desc(desc) => !desc.bases.is_empty(),
240239
}
241240
}
@@ -247,7 +246,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
247246
let entity = entity.get_template().unwrap_or(entity);
248247
entity.walk_bases_while(|child| {
249248
out.push(Self::new(Self::definition_entity(child), gen_env));
250-
WalkAction::Continue
249+
ControlFlow::Continue(())
251250
});
252251
out.into()
253252
}
@@ -318,14 +317,14 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
318317
}
319318

320319
pub fn has_methods(&self) -> bool {
321-
self.for_each_method(|_| WalkAction::Interrupt).is_interrupted()
320+
self.for_each_method(|_| ControlFlow::Break(())).is_break()
322321
}
323322

324323
#[inline]
325-
pub fn for_each_method(&self, mut predicate: impl FnMut(Func<'tu, 'ge>) -> WalkAction) -> WalkResult {
324+
pub fn for_each_method(&self, mut predicate: impl FnMut(Func<'tu, 'ge>) -> ControlFlow<()>) -> ControlFlow<()> {
326325
match self {
327326
&Self::Clang { entity, gen_env, .. } => entity.walk_methods_while(|f| predicate(Func::new(f, gen_env))),
328-
Self::Desc(_) => WalkResult::Completed,
327+
Self::Desc(_) => ControlFlow::Continue(()),
329328
}
330329
}
331330

@@ -342,11 +341,11 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
342341
for spec in specs {
343342
out.push(func.clone().specialize(spec));
344343
}
345-
return WalkAction::Continue;
344+
return ControlFlow::Continue(());
346345
}
347346
}
348347
out.push(func);
349-
WalkAction::Continue
348+
ControlFlow::Continue(())
350349
});
351350
let rust_module = match self {
352351
Class::Clang { gen_env, .. } => gen_env.module(),
@@ -364,39 +363,39 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
364363
}
365364

366365
pub fn has_fields(&self) -> bool {
367-
self.for_each_field(|_| WalkAction::Interrupt).is_interrupted()
366+
self.for_each_field(|_| ControlFlow::Break(())).is_break()
368367
}
369368

370369
#[inline]
371-
pub fn for_each_field(&self, mut predicate: impl FnMut(Field<'tu, 'ge>) -> WalkAction) -> WalkResult {
370+
pub fn for_each_field(&self, mut predicate: impl FnMut(Field<'tu, 'ge>) -> ControlFlow<()>) -> ControlFlow<()> {
372371
match self {
373372
&Self::Clang { entity, gen_env, .. } => entity.walk_fields_while(|f| predicate(Field::new(f, gen_env))),
374-
Self::Desc(_) => WalkResult::Completed,
373+
Self::Desc(_) => ControlFlow::Continue(()),
375374
}
376375
}
377376

378377
pub fn fields(&self) -> Vec<Field<'tu, 'ge>> {
379378
let mut out = Vec::with_capacity(32);
380379
self.for_each_field(|f| {
381380
out.push(f);
382-
WalkAction::Continue
381+
ControlFlow::Continue(())
383382
});
384383
out
385384
}
386385

387386
#[inline]
388-
pub fn for_each_const(&self, mut predicate: impl FnMut(Const<'tu>) -> WalkAction) -> WalkResult {
387+
pub fn for_each_const(&self, mut predicate: impl FnMut(Const<'tu>) -> ControlFlow<()>) -> ControlFlow<()> {
389388
match self {
390389
&Self::Clang { entity, .. } => entity.walk_consts_while(|f| predicate(Const::new(f))),
391-
Self::Desc(_) => WalkResult::Completed,
390+
Self::Desc(_) => ControlFlow::Continue(()),
392391
}
393392
}
394393

395394
pub fn consts(&self) -> Vec<Const<'tu>> {
396395
let mut out = Vec::with_capacity(8);
397396
self.for_each_const(|c| {
398397
out.push(c);
399-
WalkAction::Continue
398+
ControlFlow::Continue(())
400399
});
401400
out
402401
}

0 commit comments

Comments
 (0)