Skip to content

Commit

Permalink
Support continue / break with label
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Nov 1, 2024
1 parent a37867e commit 16af1cd
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ macro_rules! or_return_quiet {
}

/// Unwrap or continue with a warning.
///
/// Accepts an optional 'label as the first argument.
#[macro_export]
macro_rules! or_continue {
($expr:expr $(,)?) => {
Expand All @@ -266,9 +268,21 @@ macro_rules! or_continue {
}
}
};

($label:lifetime, $expr:expr $(,)?) => {
match $crate::Success::success($expr) {
Some(x) => x,
None => {
$crate::__log_on_bail!($expr);
continue $label;
}
}
};
}

/// Unwrap or continue quietly.
///
/// Accepts an optional 'label as the first argument.
#[macro_export]
macro_rules! or_continue_quiet {
($expr:expr $(,)?) => {
Expand All @@ -277,9 +291,18 @@ macro_rules! or_continue_quiet {
None => continue,
}
};

($label:lifetime, $expr:expr $(,)?) => {
match $crate::Success::success($expr) {
Some(x) => x,
None => continue $label,
}
};
}

/// Unwrap or break with a warning.
///
/// Accepts an optional 'label as the first argument.
#[macro_export]
macro_rules! or_break {
($expr:expr $(,)?) => {
Expand All @@ -291,9 +314,21 @@ macro_rules! or_break {
}
}
};

($label:lifetime, $expr:expr $(,)?) => {
match $crate::Success::success($expr) {
Some(x) => x,
None => {
$crate::__log_on_bail!($expr);
break $label;
}
}
};
}

/// Unwrap or break quietly.
///
/// Accepts an optional 'label as the first argument.
#[macro_export]
macro_rules! or_break_quiet {
($expr:expr $(,)?) => {
Expand All @@ -302,6 +337,13 @@ macro_rules! or_break_quiet {
None => break,
}
};

($label:lifetime, $expr:expr $(,)?) => {
match $crate::Success::success($expr) {
Some(x) => x,
None => break $label,
}
};
}

#[cfg(test)]
Expand Down

0 comments on commit 16af1cd

Please sign in to comment.