Skip to content
Myr

Merge pull request #3 from dtolnay/literal

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Accept string literal in bail macro

authored by David Tolnay · committed by GitHub · parents 6fa503d, 08b3d5d · Browse files · 2 files+31 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+31 added−0 removed

  • tests:+28 added−0 removed
  • other:+3 added−0 removed

2 files+31 added−0 removed

modified:src/lib.rs+3 added
281 unmodified lines
282/// # }283/// ```284#[macro_export]285macro_rules! bail {286 ($msg:literal $(,)?) => {287 return std::result::Result::Err($crate::anyhow!($msg));288 };289 ($err:expr $(,)?) => {290 return std::result::Result::Err(std::convert::From::from($err));291 };292 ($fmt:expr, $($arg:tt)*) => {
58 unmodified lines
added:tests/macros.rs+28 added
1use anyhow::{bail, Result};2use std::io;34fn bail_literal() -> Result<()> {5 bail!("oh no!");6}78fn bail_fmt() -> Result<()> {9 bail!("{} {}!", "oh", "no");10}1112fn bail_error() -> Result<()> {13 bail!(io::Error::new(io::ErrorKind::Other, "oh no!"));14}1516#[test]17fn test_messages() {18 assert_eq!("oh no!", bail_literal().unwrap_err().to_string());19 assert_eq!("oh no!", bail_fmt().unwrap_err().to_string());20 assert_eq!("oh no!", bail_error().unwrap_err().to_string());21}2223#[test]24fn test_downcast() {25 assert_eq!("oh no!", bail_literal().unwrap_err().downcast::<&str>().unwrap());26 assert_eq!("oh no!", bail_fmt().unwrap_err().downcast::<String>().unwrap());27 assert_eq!("oh no!", bail_error().unwrap_err().downcast::<io::Error>().unwrap().to_string());28}