breakable_block/lib.rs
1//! A shim library for a stable implementation of what is proposed in [RFC 2046](https://rust-lang.github.io/rfcs/2046-label-break-value.html),
2//! allowing for short-circuit control flow without needing to return in a
3//! function or break in a loop.
4//!
5//! When the RFC is stabilized, this crate will be deprecated.
6//! If you don't need to work on `stable`, you can use
7//! `#![feature(label_break_value)]` in your crate root to enable the
8//! functionality instead.
9//!
10//! This crate has no dependencies.
11//!
12//! ## Example
13//! Here's an example, [lifted straight from the RFC documentation](https://rust-lang.github.io/rfcs/2046-label-break-value.html#motivation)
14//! with only minor modifications:
15//!
16//! ```rust
17//! use breakable_block::breakable;
18//!
19//! # fn do_thing() {}
20//! # fn do_next_thing() {}
21//! # fn do_last_thing() {}
22//! # fn condition_not_met() -> bool { true }
23//! breakable!('block: {
24//! do_thing();
25//! if condition_not_met() {
26//! break 'block;
27//! }
28//! do_next_thing();
29//! if condition_not_met() {
30//! break 'block;
31//! }
32//! do_last_thing();
33//! });
34//! ```
35
36// No std dependency
37#![no_std]
38
39/// A breakable block, similar to [RFC 2046](https://rust-lang.github.io/rfcs/2046-label-break-value.html).
40///
41/// See the crate-level documentation for more information.
42#[macro_export]
43macro_rules! breakable {
44 ($label:lifetime: $block:block) => {
45 #[allow(clippy::never_loop)]
46 $label: loop {
47 break {
48 $block
49 }
50 }
51 };
52}