DEV Community

Discussion on: My advocation for MultiType Return-Position `impl Trait` (MTRPIT) in Rust.

Collapse
 
baenencalin profile image
Calin Baenen • Edited

Thanks kindly to "Madfrog" (@konnorandrews), who wrote an example of what the generated output code may look like in this Discord message:

trait Test {
  fn test(&self) {
    println!("Default impl!");
  }
}



struct Wibble;
impl Test for Wibble {
  fn test(&self) {
    println!("Wibble!");
  }
}

struct Wobble;
impl Test for Wobble {
  fn test(&self) {
    println!("Wobble!");
  }
}



fn get_test_value(wibble: bool) -> impl Test {
  mod __impl_enum {
    use super::*;

    pub enum __return {
      _0(Wibble),
      _1(Wobble),
    }

    impl Test for __return {
      fn test(&self) {
        match self {
          Self::_0(value) => value.test(),
          Self::_1(value) => value.test(),
        }
      }
    }
  }

  if wibble {
    __impl_enum::__return::_0(Wibble)
  } else {
    __impl_enum::__return::_1(Wobble)
  }
}

fn main() {
    get_test_value(true).test();
    get_test_value(false).test();
}
Enter fullscreen mode Exit fullscreen mode

(From the Rustlang Discord guild.)