Guest User

Untitled

a guest
Mar 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. #![feature(specialization)]
  2.  
  3. pub struct MemberBase {
  4. id: u32,
  5. }
  6. impl MemberBase {
  7. pub fn id(&self) -> u32 { self.id }
  8. }
  9. pub trait UiMemberSpecialization {
  10. fn size(&self) -> (u32, u32);
  11. }
  12. pub trait UiMember {
  13. fn id(&self) -> u32;
  14.  
  15. fn size(&self) -> (u32, u32);
  16.  
  17. fn as_member_base(&self) -> &MemberBase;
  18. fn as_member_base_mut(&mut self) -> &mut MemberBase;
  19. fn is_control(&self) -> Option<&UiControl>;
  20. fn is_control_mut(&mut self) -> Option<&mut UiControl>;
  21. }
  22. pub struct Member<T: UiMemberSpecialization + Sized> {
  23. base: MemberBase,
  24. inner: T,
  25. }
  26. impl <T: UiMemberSpecialization + Sized> UiMember for Member<T> {
  27. fn id(&self) -> u32 { self.base.id }
  28.  
  29. fn size(&self) -> (u32, u32) { self.inner.size() }
  30.  
  31. fn as_member_base(&self) -> &MemberBase { &self.base }
  32. fn as_member_base_mut(&mut self) -> &mut MemberBase { &mut self.base }
  33. default fn is_control(&self) -> Option<&UiControl> { None }
  34. default fn is_control_mut(&mut self) -> Option<&mut UiControl> { None }
  35. }
  36. // =========================================================
  37. pub struct ControlBase {
  38. name: String
  39. }
  40. impl ControlBase {
  41. pub fn name(&self) -> &str { self.name.as_ref() }
  42. }
  43. pub trait UiControlSpecialization: UiMemberSpecialization {
  44. fn parent_mut(&mut self) -> Option<&mut UiMember>;
  45. }
  46. pub trait UiControl: UiMember {
  47. fn name(&self) -> &str;
  48. fn parent_mut(&mut self) -> Option<&mut UiMember>;
  49. fn as_member(&self) -> &UiMember;
  50. fn as_member_mut(&mut self) -> &mut UiMember;
  51. fn as_control_base(&self) -> &ControlBase;
  52. fn as_control_base_mut(&mut self) -> &mut ControlBase;
  53. }
  54. pub struct Control<T: UiControlSpecialization + Sized> {
  55. base: ControlBase,
  56. inner: T,
  57. }
  58. impl <T: UiControlSpecialization + Sized> UiMemberSpecialization for Control<T> {
  59. fn size(&self) -> (u32, u32) {
  60. self.inner.size()
  61. }
  62. }
  63. impl <T: UiControlSpecialization + Sized> UiControl for Member<Control<T>> {
  64. fn name(&self) -> &str { self.inner.base.name() }
  65. fn parent_mut(&mut self) -> Option<&mut UiMember> { self.inner.inner.parent_mut() }
  66. fn as_member(&self) -> &UiMember { self }
  67. fn as_member_mut(&mut self) -> &mut UiMember { self }
  68. fn as_control_base(&self) -> &ControlBase { &self.inner.base }
  69. fn as_control_base_mut(&mut self) -> &mut ControlBase { &mut self.inner.base }
  70. }
  71. impl <T: UiControlSpecialization + Sized> UiMember for Member<Control<T>> {
  72. fn is_control(&self) -> Option<&UiControl> { Some(self) }
  73. fn is_control_mut(&mut self) -> Option<&mut UiControl> { Some(self) }
  74. }
  75.  
  76. // ==========================================================
  77. pub trait UiWindowSpecialization: UiMemberSpecialization {
  78. fn with_name(name: String) -> Self;
  79. fn on_top(&mut self);
  80. }
  81. pub trait UiWindow: UiMember {
  82. fn on_top(&mut self);
  83. fn as_member(&self) -> &UiMember;
  84. fn as_member_mut(&mut self) -> &mut UiMember;
  85. }
  86. impl <T: UiWindowSpecialization + Sized> UiWindow for Member<T> {
  87. fn on_top(&mut self) { self.inner.on_top() }
  88. fn as_member(&self) -> &UiMember { self }
  89. fn as_member_mut(&mut self) -> &mut UiMember { self }
  90. }
  91. impl <T: UiWindowSpecialization + Sized> Member<T> {
  92. pub fn with_name(name: String) -> Box<Self> {
  93. Box::new(Member {base: MemberBase {id: 42}, inner: T::with_name(name)})
  94. }
  95. }
  96. // ==========================================================
  97. pub trait UiButtonSpecialization: UiControlSpecialization {
  98. fn with_label(label: String) -> Self;
  99. fn on_click(&mut self);
  100. }
  101. pub trait UiButton: UiControl {
  102. fn on_click(&mut self);
  103. fn as_control(&self) -> &UiControl;
  104. fn as_control_mut(&mut self) -> &mut UiControl;
  105. }
  106. impl <T: UiButtonSpecialization + Sized> UiButton for Member<Control<T>> {
  107. fn on_click(&mut self) { self.inner.inner.on_click(); }
  108. fn as_control(&self) -> &UiControl { self }
  109. fn as_control_mut(&mut self) -> &mut UiControl { self }
  110. }
  111. impl <T: UiButtonSpecialization + Sized> Member<Control<T>> {
  112. pub fn with_label(label: String) -> Box<Self> {
  113. Box::new(Member {base: MemberBase {id: 55}, inner: Control { base: ControlBase {name: "Oops".to_string() }, inner: T::with_label(label) }})
  114. }
  115. }
  116. // ==========================================================
  117. pub struct WindowSpecialization {
  118. size: (u32, u32)
  119. }
  120. impl UiWindowSpecialization for WindowSpecialization {
  121. fn on_top(&mut self) { println!("Window on top!") }
  122. fn with_name(name: String) -> Self { WindowSpecialization { size: (11, 11) } }
  123. }
  124. impl UiMemberSpecialization for WindowSpecialization {
  125. fn size(&self) -> (u32, u32) { self.size }
  126. }
  127. pub type Window = Member<WindowSpecialization>;
  128. // ==========================================================
  129. pub struct ButtonSpecialization {
  130. size: (u32, u32)
  131. }
  132. impl UiButtonSpecialization for ButtonSpecialization {
  133. fn on_click(&mut self) { println!("clicked!") }
  134. fn with_label(label: String) -> Self { ButtonSpecialization {size: (99, 99)} }
  135. }
  136. impl UiControlSpecialization for ButtonSpecialization {
  137. fn parent_mut(&mut self) -> Option<&mut UiMember> { None }
  138. }
  139. impl UiMemberSpecialization for ButtonSpecialization {
  140. fn size(&self) -> (u32, u32) { self.size }
  141. }
  142. pub type Button = Member<Control<ButtonSpecialization>>;
  143. // ==========================================================
  144.  
  145. fn main() {
  146. let mut window = Window::with_name("W".to_string());
  147. let mut button = Button::with_label("B".to_string());
  148.  
  149. let mut window: &mut UiWindow = window.as_mut();
  150. let mut window: &mut UiMember = window.as_member_mut();
  151. let mut window = window.as_member_base_mut();
  152. let mut window: &mut Window = unsafe { ::std::mem::transmute(window) };
  153.  
  154. let mut button: &mut UiButton = button.as_mut();
  155. let mut button: &mut UiControl = button.as_control_mut();
  156. let mut button: &mut UiMember = button.as_member_mut();
  157. let mut button = button.as_member_base_mut();
  158. let mut button: &mut Button = unsafe { ::std::mem::transmute(button) };
  159.  
  160. window.on_top();
  161. button.on_click();
  162. }
Add Comment
Please, Sign In to add comment