Question for alternative solution for 'return trait problem'

Hello,

I have a question about traits. I run into the the trait

Example cannot be made into an object [E0038]

. I understand the error more or less, in my case the trait has a generic method (which is not allowed as far as I understand).

What I like to do though is return this Trait from a function, so an end user can work with it, without knowing the implementation in use.

I could probably get around it by using an enum and put the implementations inside there and add the generic function on there.

But is this the best solution, are there other (perhaps nicer) alternatives, or maybe something I am not aware of?

I have added simplified code below, which shows the problem (look at the bottom where the function creates the 'Example' trait, which is not allowed).

Thank you!

trait Example {
  fn do_something<F, V>(convert: F) -> V
  where
    F: FnOnce(Box<dyn Row>) -> V;
}

trait Row {
  fn get_string(&self, name: &str) -> String;
}

struct RowImpl1 {}

struct RowImpl2 {}

impl Row for RowImpl1 {
  fn get_string(&self, name: &str) -> String {
    todo!()
  }
}

impl Row for RowImpl2 {
  fn get_string(&self, name: &str) -> String {
    todo!()
  }
}

struct Example1 {}

struct Example2 {}

impl Example for Example1 {
  fn do_something<F, V>(convert: F) -> V
  where
    F: FnOnce(Box<dyn Row>) -> V,
  {
    convert(Box::new(RowImpl1 {}))
  }
}

impl Example for Example2 {
  fn do_something<F, V>(convert: F) -> V
  where
    F: FnOnce(Box<dyn Row>) -> V,
  {
    convert(Box::new(RowImpl2 {}))
  }
}

pub fn create_example1(use1: bool) -> Box<dyn Example> {
  if use1 {
    return Box::new(Example1 {});
  }
  Box::new(Example2 {})
}

https://crates.io/crates/auto_enums

Thanks I will have a look.