on-struct.rs 417 B

123456789101112131415161718192021222324252627
  1. use intertrait::cast::*;
  2. use intertrait::*;
  3. #[cast_to(Greet)]
  4. struct Data;
  5. trait Source: CastFrom {}
  6. trait Greet {
  7. fn greet(&self);
  8. }
  9. impl Greet for Data {
  10. fn greet(&self) {
  11. println!("Hello");
  12. }
  13. }
  14. impl Source for Data {}
  15. #[test]
  16. fn test_cast_to_on_struct() {
  17. let data = Data;
  18. let source: &dyn Source = &data;
  19. let greet = source.cast::<dyn Greet>();
  20. greet.unwrap().greet();
  21. }