Skip to content

Commit c3d0a61

Browse files
Add documentation for ControlFlow and try_break!
1 parent 946462a commit c3d0a61

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

chalk-ir/src/visit.rs

+13
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@ pub use visitors::VisitExt;
1414

1515
/// An copy of the unstable `std::ops::ControlFlow` for use in Chalk visitors.
1616
pub enum ControlFlow<B, C = ()> {
17+
/// Continue in the loop, using the given value for the next iteration
1718
Continue(C),
19+
/// Exit the loop, yielding the given value
1820
Break(B),
1921
}
2022

2123
impl<B, C> ControlFlow<B, C> {
24+
/// Returns `true` if this is a `Break` variant.
2225
#[inline]
2326
pub fn is_break(&self) -> bool {
2427
matches!(*self, ControlFlow::Break(_))
2528
}
2629

30+
/// Returns `true` if this is a `Continue` variant.
2731
#[inline]
2832
pub fn is_continue(&self) -> bool {
2933
matches!(*self, ControlFlow::Continue(_))
3034
}
3135

36+
/// Converts the `ControlFlow` into an `Option` which is `Some`
37+
/// if the `ControlFlow` was `Break` and `None` otherwise.
3238
#[inline]
3339
pub fn break_value(self) -> Option<B> {
3440
match self {
@@ -39,13 +45,20 @@ impl<B, C> ControlFlow<B, C> {
3945
}
4046

4147
impl<B> ControlFlow<B, ()> {
48+
/// It's frequently the case that there's no value needed with `Continue`,
49+
/// so this provides a way to avoid typing `(())`, if you prefer it.
4250
pub const CONTINUE: Self = ControlFlow::Continue(());
4351
}
4452

4553
impl<C> ControlFlow<(), C> {
54+
/// APIs like `try_for_each` don't need values with `Break`,
55+
/// so this provides a way to avoid typing `(())`, if you prefer it.
4656
pub const BREAK: Self = ControlFlow::Break(());
4757
}
4858

59+
/// Unwraps a `ControlFlow` or propagates its `Break` value.
60+
/// This replaces the `Try` implementation that would be used
61+
/// with `std::ops::ControlFlow`.
4962
#[macro_export]
5063
macro_rules! try_break {
5164
($expr:expr) => {

0 commit comments

Comments
 (0)