Advertisement
Guest User

Untitled

a guest
May 29th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.80 KB | None | 0 0
  1. use anyhow::{anyhow, Ok, Result};
  2.  
  3. struct MyType;
  4.  
  5. trait MyTrait1: Sized {
  6.     fn foo(my_type: MyType) -> Result<Self> {
  7.         Ok(Self::try_from_my_type(my_type)?)
  8.     }
  9.  
  10.     fn try_from_my_type(my_type: MyType) -> Result<Self>;
  11. }
  12.  
  13. impl<T> MyTrait1 for T
  14. where
  15.     T: Sized + TryFrom<MyType>,
  16. {
  17.     fn try_from_my_type(my_type: MyType) -> Result<Self> {
  18.         Self::try_from(my_type).map_err(|_| anyhow!("Failed to convert"))
  19.     }
  20. }
  21.  
  22. trait MyTrait2: Sized {
  23.     fn foo(my_type: MyType) -> Result<Self> {
  24.         Ok(Self::try_from_my_type(my_type)?)
  25.     }
  26.  
  27.     fn try_from_my_type(my_type: MyType) -> Result<Self>;
  28. }
  29.  
  30. impl<T> MyTrait2 for T
  31. where
  32.     T: Sized + From<MyType>,
  33. {
  34.     fn try_from_my_type(my_type: MyType) -> Result<Self> {
  35.         Ok(Self::from(my_type))
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement