Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. trait Ext {
  2. fn test(&self);
  3. }
  4.  
  5.  
  6. // A
  7.  
  8.  
  9. struct A(String);
  10.  
  11.  
  12. impl A {
  13. fn new(v: impl AsRef<str>) -> A { A(v.as_ref().to_owned()) }
  14. }
  15.  
  16.  
  17. impl Ext for A {
  18. fn test(&self) {
  19. println!("A [{}]: Hello, world!", self.0);
  20. }
  21. }
  22.  
  23.  
  24. // B with A inside
  25.  
  26.  
  27. struct B<T>(String, T);
  28.  
  29.  
  30. impl<T: Ext> B<T> {
  31. fn new(v: impl AsRef<str>, inner: T) -> B<T> { B(v.as_ref().to_owned(), inner) }
  32. }
  33.  
  34.  
  35. impl<T: Ext> Ext for B<T> {
  36. fn test(&self) {
  37. self.1.test();
  38. println!("B [{}]: Hello, world!", self.0);
  39. }
  40. }
  41.  
  42.  
  43. // APP
  44.  
  45.  
  46. struct App {
  47. inner: Box<dyn Ext>,
  48. }
  49.  
  50.  
  51. impl App {
  52. fn test(&self) { self.inner.test() }
  53. }
  54.  
  55.  
  56. fn main() {
  57. let app = App {
  58. inner: Box::new(A::new("test-1")),
  59. };
  60.  
  61. // TODO:...
  62.  
  63. app.test();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement