on-enum.rs 470 B

12345678910111213141516171819202122232425262728293031
  1. use intertrait::cast::*;
  2. use intertrait::*;
  3. #[cast_to(Greet)]
  4. #[allow(dead_code)]
  5. enum Data {
  6. Var1,
  7. Var2(u32),
  8. }
  9. trait Source: CastFrom {}
  10. trait Greet {
  11. fn greet(&self);
  12. }
  13. impl Greet for Data {
  14. fn greet(&self) {
  15. println!("Hello");
  16. }
  17. }
  18. impl Source for Data {}
  19. #[test]
  20. fn test_cast_to_on_enum() {
  21. let data = Data::Var2(1);
  22. let source: &dyn Source = &data;
  23. let greet = source.cast::<dyn Greet>();
  24. greet.unwrap().greet();
  25. }