You are trying to go through too many hoops: from some concrete type
T : ToString + 'static (such as
T = &'static str for instance), you want to go to
Box<T> then coerce to
Box<dyn ToString + 'static> then wrap that in a
Some() with the
impl Into<Option< ... >> genericity.
That's far too much for Rust (mainly the
Boxing thing, since a heap-allocation should be explicit).
So, by skipping the whole "
Box for me", you can have a signature that does the rest (coercion and
fn call_trait_object<T : ToString + 'static> (
val: impl Into<Option< Box<T> >>,
let val: Option<Box<dyn ToString + 'static>> =
val.into().map(|it| it as _)
fn main ()
call_trait_object(None) fails because of type inference...
Option is to keep the signature you had, but help it when providing a parameter:
call_trait_object(Box::new("some text") as Box<dyn ToString>); // no error