@@ -132,3 +132,54 @@ impl<T: fmt::Debug> ::std::error::Error for TestError<T> {
132132 }
133133 }
134134}
135+
136+ mod private {
137+ pub trait Sealed {}
138+
139+ impl<T, E> Sealed for Result<T, E> {}
140+ }
141+
142+ /// Extension trait for `Result<T, E>` to provide additional functionality
143+ /// specifically for prop test cases.
144+ pub trait ProptestResultExt<T, E>: private::Sealed {
145+ /// Converts a `Result<T, E>` into a `Result<T, TestCaseError>`, where the
146+ /// `Err` case is transformed into a `TestCaseError::Reject`.
147+ ///
148+ /// This is intended to be used like the [`prop_assume!`] macro, but for
149+ /// fallible computations. If the result is `Err`, the test input is rejected
150+ /// and a new input will be generated.
151+ ///
152+ /// ## Example
153+ ///
154+ /// ```
155+ /// use proptest::prelude::*;
156+ /// proptest! {
157+ /// #[test]
158+ /// fn test_that_only_works_with_positive_integers(a in -10i32..10i32) {
159+ /// // Reject the case if `a` cannot be converted to u8 (e.g., negative values)
160+ /// let _unsigned: u8 = a.try_into().prop_assume_ok()?;
161+ /// // ...rest of test...
162+ /// }
163+ /// }
164+ /// #
165+ /// # fn main() { test_signed_to_unsigned(); }
166+ /// ```
167+ ///
168+ /// [`prop_assume!`]: crate::prop_assume
169+ fn prop_assume_ok(self) -> Result<T, TestCaseError>
170+ where
171+ E: fmt::Debug;
172+ }
173+
174+ impl<T, E> ProptestResultExt<T, E> for Result<T, E> {
175+ #[track_caller]
176+ fn prop_assume_ok(self) -> Result<T, TestCaseError>
177+ where
178+ E: fmt::Debug,
179+ {
180+ let location = core::panic::Location::caller();
181+ self.map_err(|err| {
182+ TestCaseError::reject(format!("{location}: {err:?}"))
183+ })
184+ }
185+ }
0 commit comments