#guard #try #macro

try-guard

A guard! macro inspired by the guard Alternative function from Haskell

3 unstable releases

0.2.0 Jun 7, 2019
0.1.1 May 7, 2019
0.1.0 May 7, 2019

#1346 in Rust patterns

Download history 33/week @ 2024-03-24 86/week @ 2024-03-31 21/week @ 2024-04-07 37/week @ 2024-04-14 62/week @ 2024-04-21 26/week @ 2024-04-28 20/week @ 2024-05-05 42/week @ 2024-05-12 54/week @ 2024-05-19 38/week @ 2024-05-26 47/week @ 2024-06-02 44/week @ 2024-06-09 139/week @ 2024-06-16 28/week @ 2024-06-23 29/week @ 2024-06-30 110/week @ 2024-07-07

314 downloads per month
Used in 3 crates

BSD-3-Clause

9KB

The guard Haskell Alternative function brought to Rust

The guard! macro.

The guard! macro implements a control-flow sugar that occurs very often in common Rust code:

fn foo(cond: bool) -> Option<i32> {
  if !cond {
    return None;
  }

  // do something useful

  Some(42)
}

This pattern of testing arguments and early-returning with an error is very typical. Unfortunately, the ? operator doesn’t help us here because we want to early-return on a boolean value, not an error value.

A not very idiomatic and weird way to rewrite that:

fn foo(cond: bool) -> Option<i32> {
  if cond { Some(()) } else { None }?;
  Some(42)
}

This crate provides the guard! macro — analoguous to the guard Haskell Alternative function — that helps early-return from a function if a predicate is false:


use try_guard::guard;

fn foo(cond: bool) -> Option<i32> {
  guard!(cond);
  Some(42)
}

Custom guard types

This crate also allows you to guard to any thing that implements Try<Error = NoneError>. For instance, the following works:


use std::ops::Try;
use std::option::NoneError;
use try_guard::guard;

enum MyGuard<T> {
  Just(T),
  Nothing
}

impl<T> MyGuard<T> {
  fn new(x: T) -> Self {
    MyGuard::Just(x)
  }

  fn none() -> Self {
    MyGuard::Nothing
  }
}

impl<T> Try for MyGuard<T> {
  type Ok = T;

  type Error = NoneError;

  fn from_error(_: Self::Error) -> Self {
    MyGuard::none()
  }

  fn from_ok(x: Self::Ok) -> Self {
    MyGuard::new(x)
  }

  fn into_result(self) -> Result<Self::Ok, Self::Error> {
    match self {
      MyGuard::Just(x) => Ok(x),
      MyGuard::Nothing => Err(NoneError)
    }
  }
}

fn foo(cond: bool) -> MyGuard<i32> {
  guard!(cond);
  MyGuard::new(42)
}

Feature flags

  • The test-nightly feature flag can be used to test nightly-related features that come freely and don’t require a nightly build of rustc to compile this crate but require one at use site.

No runtime deps

Features