title: Using Any with traits in Rust
body: <p>I'm trying to implement <code>PartialEq</code> in Rust for a trait that has subtypes, so that I can add them as boxed pointers to a container and later compare them.</p>
  
  <p>Here's my scaled-down implementation:</p>
  
  <pre><code>use std::any::Any;
  
  trait Foo: Any {}
  
  struct Bar {}
  
  impl Foo for Bar {}
  
  struct Baz {}
  
  impl Foo for Baz {}
  
  impl PartialEq for Foo {
      fn eq(&amp;self, other: &amp;Foo) -&gt; bool {
          let me = self as &amp;Any;
          let you = other as &amp;Any;
          if me.is::&lt;Bar&gt;() &amp;&amp; you.is::&lt;Bar&gt;() {
              true
          } else if me.is::&lt;Baz&gt;() &amp;&amp; you.is::&lt;Baz&gt;() {
              true
          } else {
              false
          }
      }
  }
  
  fn main() {
      let bar: Bar = Bar {};
      let baz: Baz = Baz {};
      let foo1: &amp;Foo = &amp;bar;
      let foo2: &amp;Foo = &amp;baz;
      println!("{:?}", foo1 == foo2);
  }
  </code></pre>
  
  <p><a href="https://play.rust-lang.org/?code=use%20std%3A%3Aany%3A%3AAny%3B%0A%0Atrait%20Foo%3A%20Any%20%7B%7D%0A%0Astruct%20Bar%20%7B%7D%0A%0Aimpl%20Foo%20for%20Bar%20%7B%7D%0A%0Astruct%20Baz%20%7B%7D%0A%0Aimpl%20Foo%20for%20Baz%20%7B%7D%0A%0Aimpl%20PartialEq%20for%20Foo%20%7B%0A%20%20%20%20fn%20eq(%26self%2C%20other%3A%20%26Foo)%20-%3E%20bool%20%7B%0A%20%20%20%20%20%20%20%20let%20me%20%3D%20self%20as%20%26Any%3B%0A%20%20%20%20%20%20%20%20let%20you%20%3D%20other%20as%20%26Any%3B%0A%20%20%20%20%20%20%20%20if%20me.is%3A%3A%3CBar%3E()%20%26%26%20you.is%3A%3A%3CBar%3E()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20true%0A%20%20%20%20%20%20%20%20%7D%20else%20if%20me.is%3A%3A%3CBaz%3E()%20%26%26%20you.is%3A%3A%3CBaz%3E()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20true%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20false%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20bar%3A%20Bar%20%3D%20Bar%20%7B%7D%3B%0A%20%20%20%20let%20baz%3A%20Baz%20%3D%20Baz%20%7B%7D%3B%0A%20%20%20%20let%20foo1%3A%20%26Foo%20%3D%20%26bar%3B%0A%20%20%20%20let%20foo2%3A%20%26Foo%20%3D%20%26baz%3B%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20foo1%20%3D%3D%20foo2)%3B%0A%7D%0A&amp;version=nightly&amp;backtrace=0" rel="noreferrer">Code example in Rust Playground</a>.</p>
  
  <p>When I build this, I get:</p>
  
  <pre><code>rustc 1.17.0-nightly (0648517fa 2017-02-03)
  error: non-scalar cast: `&amp;Foo + 'static` as `&amp;std::any::Any + 'static`
    --&gt; &lt;anon&gt;:15:18
     |
  15 |         let me = self as &amp;Any;
     |                  ^^^^^^^^^^^^
  
  error: non-scalar cast: `&amp;Foo + 'static` as `&amp;std::any::Any + 'static`
    --&gt; &lt;anon&gt;:16:19
     |
  16 |         let you = other as &amp;Any;
     |                   ^^^^^^^^^^^^^
  
  error: aborting due to 2 previous errors
  </code></pre>
  
  <p>which is confusing. Any ideas what I'm doing wrong here?</p>
  
  <p><strong>Edit</strong>: I don't believe this is a duplicate of <a href="https://stackoverflow.com/questions/28632968">Why doesn't Rust support trait object upcasting?</a>, because what I'm trying to do is downcast using <code>Any</code>, not upcast.</p>
  
  <p><strong>Further Edit</strong>: Yes, this is a duplicate - sorry, I was thinking about what I was trying to do (downcast to the <code>Bar</code> and <code>Baz</code> types) rather than how I was doing that (upcasting to <code>Any</code>). However, that being said, I guess I still don't understand why the <a href="https://doc.rust-lang.org/std/any/" rel="noreferrer">Any example</a>, where they do this: <code>let value_any = value as &amp;Any;</code> works, where mine doesn't. That being said, <a href="https://stackoverflow.com/users/3179290/joshua-entrekin">Joshua Entrekin</a> did give a great answer.</p>
  
  <p><strong>Final Edit</strong> An, never mind, it's because I'm upcasting a trait rather than a type - <strong>Doh!</strong>. Thanks, everyone!</p>