Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. use std::convert::TryFrom;
  2.  
  3. struct Numeric(u64);
  4.  
  5. enum SomeType {
  6. Numeric(Numeric),
  7. }
  8.  
  9. macro_rules! impl_try_from {
  10. ($ty:ty) => {
  11. impl TryFrom<$ty> for SomeType {
  12. type Error = <u64 as TryFrom<$ty>>::Error;
  13.  
  14. fn try_from(n: $ty) -> Result<Self, Self::Error> {
  15. u64::try_from(n).map(|n| SomeType::Numeric(Numeric(n)))
  16. }
  17. }
  18. };
  19. }
  20.  
  21. impl_try_from!(u8);
  22. impl_try_from!(u16);
  23. impl_try_from!(u32);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement