#![cfg_attr(feature = "test-nightly", feature(try_trait), feature(try_blocks))] use try_guard::guard; #[test] fn success() { fn foo() -> Option { guard!(1 < 2); Some(10) } assert_eq!(foo(), Some(10)); } #[test] fn failure() { fn foo() -> Option { guard!(1 > 2); Some(10) } assert_eq!(foo(), None); } #[cfg(feature = "test-nightly")] mod nightly { use super::*; #[test] fn try_success() { let foo: Option = try { guard!(1 < 2); 10 }; assert_eq!(foo, Some(10)); } #[test] fn try_failure() { let foo: Option = try { guard!(1 > 2); 10 }; assert_eq!(foo, None); } #[derive(Debug, PartialEq)] struct CustomError; impl From for CustomError { fn from(_: std::option::NoneError) -> Self { CustomError } } #[test] fn try_result_success() { let foo: Result = try { guard!(1 < 2); 10 }; assert_eq!(foo, Ok(10)); } #[test] fn try_result_failure() { let foo: Result = try { guard!(1 > 2); 10 }; assert_eq!(foo, Err(CustomError)); } }