on-trait-impl.rs 414 B

123456789101112131415161718192021222324252627
  1. use intertrait::cast::*;
  2. use intertrait::*;
  3. struct Data;
  4. trait Source: CastFrom {}
  5. trait Greet {
  6. fn greet(&self);
  7. }
  8. #[cast_to]
  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_trait_impl() {
  17. let data = Data;
  18. let source: &dyn Source = &data;
  19. let greet = source.cast::<dyn Greet>();
  20. greet.unwrap().greet();
  21. }