Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- print_generic_info::<Header>(None);
- }
- fn print_generic_info<S: for<'a> LayoutSection<'a>>(game: Option<Game>) {
- if let Some(s) = game {
- let section = s.get_section::<S>();
- // ...
- }
- }
- struct Game;
- impl Game {
- fn get_section<'a, S: LayoutSection<'a>>(&self) -> Option<&'a LayoutSection> {
- Some(match <S as LayoutSection>::section_type() {
- SectionType::Header => unimplemented!(),
- })
- }
- }
- #[derive(Debug)]
- enum SectionType {
- Header
- }
- trait LayoutSection<'a> {
- fn section_kind(&self) -> SectionType;
- fn section_type() -> SectionType
- where
- Self: Sized;
- fn print_generic_info(&self) {
- println!("Type: {:?}", self.section_kind());
- }
- }
- struct Header;
- impl<'a> LayoutSection<'a> for Header {
- fn section_kind(&self) -> SectionType {
- SectionType::Header
- }
- fn section_type() -> SectionType {
- SectionType::Header
- }
- }
Add Comment
Please, Sign In to add comment