Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. enum Error<E> {
  2. FuncFailed(E),
  3. SomethingElseFailed
  4. }
  5.  
  6. trait MaybeResult {
  7. type Error;
  8. fn into_result(self) -> Result<(), Self::Error>;
  9. }
  10.  
  11. impl<E> MaybeResult for Result<(), E> {
  12. type Error = E;
  13. fn into_result(self) -> Self { self }
  14. }
  15.  
  16. impl MaybeResult for () {
  17. type Error = std::convert::Infallible;
  18. fn into_result(self) -> Result<(), Self::Error> { Ok(()) }
  19. }
  20.  
  21. fn do_something_else() -> Result<(), ()> {
  22. Ok(())
  23. }
  24.  
  25. fn do_stuff<F: FnOnce() -> R, R: MaybeResult>(func: F) -> Result<(), Error<R::Error>> {
  26. func().into_result().map_err(Error::FuncFailed)?;
  27. do_something_else().map_err(|_| Error::SomethingElseFailed)?;
  28. Ok(())
  29. }
  30.  
  31.  
  32. fn main() {
  33. // Is there a way of making this call work without having to add explicit parameters or return types?
  34. do_stuff(|| { });
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement