Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. use std::rc::*;
  2. use std::borrow::Borrow;
  3. use std::convert::AsRef;
  4. use std::convert::Into;
  5. use std::convert::From;
  6.  
  7.  
  8.  
  9.  
  10. mod cratez {
  11. use std::rc::*;
  12. use std::borrow::Borrow;
  13. use std::convert::AsRef;
  14. use std::convert::Into;
  15. use std::convert::From;
  16.  
  17. mod private {
  18. use std::rc::*;
  19. use std::borrow::Borrow;
  20. use std::convert::AsRef;
  21. use std::convert::Into;
  22. use std::convert::From;
  23. use crate::cratez::Delegator;
  24.  
  25. #[derive(Debug)]
  26. pub struct IntoDynDelegate {
  27. pub(super) field: Rc<dyn Delegator>
  28. }
  29.  
  30. impl<D> From<Rc<D>> for IntoDynDelegate where D: Delegator + 'static {
  31. fn from(value: Rc<D>) -> Self {
  32. println!("Converting RC");
  33. Self {field: value }
  34. }
  35. }
  36.  
  37. impl<D> From<D> for IntoDynDelegate where D: Delegator + 'static {
  38. fn from(value: D) -> Self {
  39. println!("Converting Value");
  40. Rc::new(value).into()
  41. }
  42. }
  43. }
  44.  
  45.  
  46.  
  47. use crate::cratez::private::IntoDynDelegate;
  48. #[derive(Debug)]
  49. pub struct Owner {
  50. delegate: IntoDynDelegate
  51. }
  52.  
  53. impl Owner {
  54.  
  55. pub fn new<D>(delegate: D) -> Self where D: Into<IntoDynDelegate> {
  56. Owner {
  57. delegate: delegate.into()
  58. }
  59. }
  60.  
  61.  
  62. pub fn delegate(&self) {
  63. self.delegate.field.hello();
  64. }
  65. }
  66.  
  67. pub trait Delegator: std::fmt::Debug {
  68. fn hello(&self) {
  69. println!("Hello World DEFAULT IMPLEMENTATIN");
  70. }
  71. }
  72.  
  73. }
  74.  
  75. use crate::cratez::Delegator;
  76. #[derive(Debug)]
  77. pub struct Delegate {
  78. field: i32,
  79. }
  80.  
  81.  
  82. impl Delegate {
  83. pub fn new(field: i32) -> Self { Delegate { field } }
  84. }
  85.  
  86. impl Delegator for Delegate {
  87. fn hello(&self) {
  88. println!("Hello Delegate IMPLEMENTATION");
  89. }
  90. }
  91.  
  92. fn main() {
  93. use cratez::{Owner};
  94. let delegate = Delegate::new(5);
  95. let owner = Owner::new(delegate);
  96. owner.delegate();
  97.  
  98.  
  99. let delegate = Rc::new(Delegate::new(99));
  100. let owner = Owner::new(delegate);
  101. owner.delegate();
  102.  
  103. //println!("{:#?}", owner);
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement