castable_to.rs 860 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use intertrait::cast::*;
  2. use intertrait::*;
  3. struct Data;
  4. trait Source: CastFrom {}
  5. trait Greet {
  6. fn greet(&self);
  7. }
  8. impl Greet for Data {
  9. fn greet(&self) {
  10. println!("Hello");
  11. }
  12. }
  13. trait Greet1 {
  14. fn greet1(&self);
  15. }
  16. impl Greet1 for Data {
  17. fn greet1(&self) {
  18. println!("Hello1");
  19. }
  20. }
  21. trait Greet2 {
  22. fn greet2(&self);
  23. }
  24. impl Greet2 for Data {
  25. fn greet2(&self) {
  26. println!("Hello2");
  27. }
  28. }
  29. impl Source for Data {}
  30. castable_to! { Data => crate::Greet, Greet1, Greet2 }
  31. #[test]
  32. fn test_multi_traits_on_struct() {
  33. let data = Data;
  34. let source: &dyn Source = &data;
  35. let greet = source.cast::<dyn Greet>();
  36. greet.unwrap().greet();
  37. let greet1 = source.cast::<dyn Greet1>();
  38. greet1.unwrap().greet1();
  39. let greet2 = source.cast::<dyn Greet2>();
  40. greet2.unwrap().greet2();
  41. }