SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::rc::Rc;
  2. use std::any::Any;
  3.  
  4. trait Avatar {
  5.     fn draw(&self);
  6. }
  7.  
  8. struct PointAvatar {
  9.     x: i32,
  10.     y: i32,
  11. }
  12.  
  13. impl Avatar for PointAvatar {
  14.     fn draw(&self) {}
  15. }
  16.  
  17. struct APIData {
  18.     root: Rc<dyn Any>,
  19. }
  20.  
  21. fn init_api(avatar: Rc<dyn Any>) -> APIData {
  22.     APIData {root: avatar}
  23. }
  24.  
  25. fn api_draw(api: &APIData) {
  26.     let avatar = api.root.downcast::<dyn Avatar>().unwrap();
  27.     avatar.draw();
  28. }
  29.  
  30. fn main() {
  31.     let avatar: Rc<dyn Any> = Rc::new(PointAvatar {x: 0, y: 0});
  32.     let api = init_api(avatar);
  33.    
  34.     // these two lines would happen every frame or something
  35.     let avatar = avatar.downcast::<PointAvatar>().unwrap();
  36.     avatar.x += 1;
  37.    
  38.     api_draw(&api);
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top