Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Error<E> {
- FuncFailed(E),
- SomethingElseFailed
- }
- trait MaybeResult {
- type Error;
- fn into_result(self) -> Result<(), Self::Error>;
- }
- impl<E> MaybeResult for Result<(), E> {
- type Error = E;
- fn into_result(self) -> Self { self }
- }
- impl MaybeResult for () {
- type Error = std::convert::Infallible;
- fn into_result(self) -> Result<(), Self::Error> { Ok(()) }
- }
- fn do_something_else() -> Result<(), ()> {
- Ok(())
- }
- fn do_stuff<F: FnOnce() -> R, R: MaybeResult>(func: F) -> Result<(), Error<R::Error>> {
- func().into_result().map_err(Error::FuncFailed)?;
- do_something_else().map_err(|_| Error::SomethingElseFailed)?;
- Ok(())
- }
- fn main() {
- // Is there a way of making this call work without having to add explicit parameters or return types?
- do_stuff(|| { });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement