title: Unable to cast generic type implementing Any to Any
body: <p>I'm trying to store dynamic objects using <code>Any</code>. </p>
  
  <p>My problem is that the <code>add_bar</code> function does not work as I expect. I would think that I could add a <code>Bar&lt;T&gt;</code> where <code>T: Any + 'static</code> to a vector of <code>Bar&lt;Any&gt;</code>, but I get a <em>mismatched types</em> error.</p>
  
  <p>If I follow the suggestion from <a href="https://stackoverflow.com/questions/29879372/expected-trait-coreopsfnmut-found-type-parameter">here</a>, I get a <em>non-scalar cast</em> error.</p>
  
  <pre class="lang-rust prettyprint-override"><code>use std::any::Any;
  
  struct Bar&lt;S: ?Sized&gt; {
      ev: fn(S) -&gt; bool,
  }
  
  // test case
  struct Foo {
      pub hello: Vec&lt;Bar&lt;Any&gt;&gt;
  }
  
  impl Foo {
      pub fn add_bar&lt;T: Any + 'static&gt;(&amp;mut self, baz: Bar&lt;T&gt;) {
          let baz2 = baz as Bar&lt;_&gt;; // non-scalar cast
          self.hello.push(baz2); // without using baz2: mismatched types. expected trait Any, found type parameter
      }
  }
  
  fn bla() {
      let mut my_foo = Foo { hello: Vec::new() };
      fn e(v: f64) -&gt; bool {
          true
      }
      let my_bar = Bar::&lt;f64&gt; { ev: e };
      my_foo.add_bar::&lt;f64&gt;(my_bar);
  }
  </code></pre>
  
  <p>The only solution so far is to make the <code>ev</code> function take an <code>Any</code> as its argument, but that doesn't seem very elegant because all possible <code>ev</code> functions would have to contain their own downcasting.</p>