Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ilyvion/ward
Provides a ward! macro which returns the contents of an Option<T> and otherwise returns early, and a guard! macro, which does the same, but with a syntax more similar to Swift's guard syntax
https://github.com/ilyvion/ward
guard rust
Last synced: 8 days ago
JSON representation
Provides a ward! macro which returns the contents of an Option<T> and otherwise returns early, and a guard! macro, which does the same, but with a syntax more similar to Swift's guard syntax
- Host: GitHub
- URL: https://github.com/ilyvion/ward
- Owner: ilyvion
- License: mit
- Created: 2019-09-09T16:15:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-16T11:34:06.000Z (over 5 years ago)
- Last Synced: 2024-12-23T08:36:00.468Z (16 days ago)
- Topics: guard, rust
- Language: Rust
- Size: 22.5 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ward
[![Crates.io](https://img.shields.io/crates/v/ward)](https://crates.io/crates/ward)
[![Crates.io](https://img.shields.io/crates/l/ward)](https://crates.io/crates/ward)
[![Crates.io](https://img.shields.io/crates/d/ward)](https://crates.io/crates/ward)
[![Docs.io](https://docs.rs/ward/badge.svg)](https://docs.rs/ward)
![no_std](https://img.shields.io/badge/no__std-yes-brightgreen)This crate exports two macros, which are intended to replicate the functionality of Swift's
guard expression with `Option`.The `guard!` macro was created to emulate the `guard let` statement in Swift. This macro is only
really useful for moving values out of `Option`s into variables.
The `ward!` macro, on the other hand, doesn't force the creation of a variable, it only returns
the value that the `guard!` variable would place into a variable. As such, it's a more flexible
version of the `guard!` macro; and probably also somewhat more Rustic.## Examples
```rust
let sut = Some("test");// This creates the variable res, which from an Option will return a T if it is Some(T), and will
// otherwise return early from the function.
guard!(let res = sut);
assert_eq!(res, "test");
```The `ward!` macro, by comparison, just returns the value, without forcing you to make a variable
from it (although we still do in this example):```rust
let sut = Some("test");
let res = ward!(sut);
assert_eq!(res, "test");
```Both macros also support an `else` branch, which will run if the `Option` is `None`:
```rust
let sut = None;
guard!(let _res = sut, else {
println!("This will be called!");// Because sut is None, the else branch will be run. When the else branch is invoked, guard!
// no longer automatically returns early for you, so you must do so yourself if you want it.
return;
});
unreachable!();
```Both macros also support an alternative "early return statement", which will let you e.g.
`break` within loops:```rust
// Not that you couldn't (and probably should) do this case with `while let Some(res) = sut`...
let mut sut = Some(0);
loop {
let res = ward!(sut, break);
sut = if res < 5 {
Some(res + 1)
} else {
None
}
}
assert_eq!(sut, None);
```