Guest User

Untitled

a guest
Jun 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. fn main() {
  2. print_generic_info::<Header>(None);
  3. }
  4.  
  5. fn print_generic_info<S: for<'a> LayoutSection<'a>>(game: Option<Game>) {
  6. if let Some(s) = game {
  7. let section = s.get_section::<S>();
  8. // ...
  9. }
  10. }
  11.  
  12. struct Game;
  13.  
  14. impl Game {
  15. fn get_section<'a, S: LayoutSection<'a>>(&self) -> Option<&'a LayoutSection> {
  16. Some(match <S as LayoutSection>::section_type() {
  17. SectionType::Header => unimplemented!(),
  18. })
  19. }
  20. }
  21.  
  22. #[derive(Debug)]
  23. enum SectionType {
  24. Header
  25. }
  26.  
  27. trait LayoutSection<'a> {
  28. fn section_kind(&self) -> SectionType;
  29. fn section_type() -> SectionType
  30. where
  31. Self: Sized;
  32.  
  33. fn print_generic_info(&self) {
  34. println!("Type: {:?}", self.section_kind());
  35. }
  36. }
  37.  
  38. struct Header;
  39.  
  40. impl<'a> LayoutSection<'a> for Header {
  41. fn section_kind(&self) -> SectionType {
  42. SectionType::Header
  43. }
  44.  
  45. fn section_type() -> SectionType {
  46. SectionType::Header
  47. }
  48. }
Add Comment
Please, Sign In to add comment