title: Rust trait issues trait cannot be made into an object
body: <p>I'm trying to write some code that will generate a random struct with a random value. I have the following trait and helper macros for the structs:</p>
  <pre><code>use rand::{thread_rng, Rng};
  use std::fmt;
  
  pub trait DataType {
      /// generate a new instance of the type with a random value
      fn random() -&gt; Box&lt;Self&gt;;
      /// generate a new instance of the same type with a random value
      fn gen_another(&amp;self) -&gt; Box&lt;Self&gt;;
  }
  
  macro_rules! impl_data_type_for_num {
      ($x:ident) =&gt; {
          impl DataType for $x {
              fn random() -&gt; Box&lt;Self&gt; {
                  Box::new(Self {
                      value: thread_rng().gen()
                  })
              }
  
              fn gen_another(&amp;self) -&gt; Box&lt;Self&gt; {
                  Self::random()
              }
          }
      };
  }
  
  macro_rules! impl_formatting {
      ($x:ident, $s:expr) =&gt; {
          impl fmt::Debug for $x {
              fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
                  write!(f, $s)
              }
          }
  
          impl fmt::Display for $x {
              fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
                  write!(f, &quot;{}&quot;, self.value)
              }
          }
      };
  }
  </code></pre>
  <p>Then I use the macros to implement the needed traits on a bunch of structs (heres a few for example):</p>
  <pre><code>pub struct VirtBool {
      value: bool
  }
  impl_data_type_for_num!(VirtBool);
  impl_formatting!(VirtBool, &quot;bool&quot;);
  
  pub struct VirtU8 {
      value: u8
  }
  impl_data_type_for_num!(VirtU8);
  impl_formatting!(VirtU8, &quot;u8&quot;);
  
  pub struct VirtU16 {
      value: u16
  }
  impl_data_type_for_num!(VirtU16);
  impl_formatting!(VirtU16, &quot;u16&quot;);
  </code></pre>
  <p>So far it all works fine, but then an issue arises when I try to implement the same traits on a struct with unsized fields:</p>
  <pre><code>pub struct VirtArray {
      _type: Box&lt;dyn DataType&gt;,
      value: Vec&lt;Box&lt;dyn DataType&gt;&gt;
  }
  impl DataType for VirtArray {
      fn random() -&gt; Box&lt;Self&gt; {
          let t = random_var();
          let s = thread_rng().gen_range(0, 10);
          Box::new(Self {
              _type: *t,
              value: (0..s).map(|_| t.gen_another()).collect()
          })
      }
  
      fn gen_another(&amp;self) -&gt; Box&lt;Self&gt; {
          Box::new(Self {
              _type: self._type,
              value: self.value.iter().map(|t| t.gen_another()).collect::&lt;Vec&lt;Box&lt;dyn DataType&gt;&gt;&gt;()
          })
      }
  }
  impl fmt::Debug for VirtArray {
      fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
          write!(f, &quot;[{:?}; {}]&quot;, self._type, self.value.len())
      }
  }
  impl fmt::Display for VirtArray {
      fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
          let mut s = self.value.iter().map(|v| format!(&quot;{}, &quot;,v)).collect::&lt;String&gt;();
          s.truncate(s.len().checked_sub(2).unwrap_or(s.len()));
          write!(f, &quot;[{}]&quot;, s)
      }
  }
  </code></pre>
  <p>This throws the error <code>the trait 'DataType' cannot be made into an object</code> on several occasions. I then have a function to create a random struct but that also throws the same error:</p>
  <pre><code>/// generate a random type with random value
  fn random_var() -&gt; Box&lt;dyn DataType&gt; {
      match thread_rng().gen_range(0,4) {
          0  =&gt; Box::new(VirtBool::random()),
          1  =&gt; Box::new(VirtU8::random()),
          2  =&gt; Box::new(VirtU16::random()),
          3  =&gt; Box::new(VirtArray::random()),
          _  =&gt; panic!(&quot;invalid&quot;)
      }
  }
  </code></pre>
  <p>I was originally using enums to do all of this but I'm trying to switch it over to structs and traits to help with scaling/use-ability. Does anyone have any idea how to fix the code above? I've been at a roadblock here for quite a while now.</p>
  <p>Also, I'm aware I could use <code>type_name_of_val</code> to print the types, but I'm trying to keep from using unstable/nightly features.</p>