title: Sized and using traits as sized objects
body: <pre><code>struct Point { x: f64, y: f64 }
  struct Circle { center: Point, radius: f64 }
  struct Square { lowerLeftCorner: Point, side: f64 }
  
  trait ShapeVisitor {
      fn visit_circle(&amp;mut self, c: &amp;Circle);
      fn visit_square(&amp;mut self, c: &amp;Square);
  }
  
  trait Shape {
      fn accept&lt;V: ShapeVisitor&gt;(&amp;self, sv: &amp;mut V);
  }
  
  impl Shape for Circle {
      fn accept&lt;V: ShapeVisitor&gt;(&amp;self, sv: &amp;mut V) {
          sv.visit_circle(self);
      }
  }
  
  impl Shape for Square {
      fn accept&lt;V: ShapeVisitor&gt;(&amp;self, sv: &amp;mut V) {
          sv.visit_square(self);
      }
  }
  </code></pre>
  <p>So here's a method that computes the total area:</p>
  <pre><code>fn area(shapes: Vec&lt;Box&lt;dyn Shape&gt;&gt;) -&gt; f64 {
      struct AreaCalculator {
          area: f64,
      }
      impl ShapeVisitor for AreaCalculator {
          fn visit_circle(&amp;mut self, c: &amp;Circle) {
              self.area += std::f64::consts::PI * c.radius * c.radius;
          }
          fn visit_square(&amp;mut self, r: &amp;Square) {
              self.area += r.side * r.side;
          }
      }
      let mut calculator = AreaCalculator { area: 0.0 };
      for shape in shapes {
          (*shape).accept(calculator);
      }
      calculator.area
  }
  </code></pre>
  <p>And here's an example program</p>
  <pre><code>fn main() {
      let mut shapes: Vec&lt;Box&lt;dyn Shape&gt;&gt; = Vec::new();
      let p = Point { x: 0.0, y: 0.0 };
      let circle = Circle { center: p, radius: 12.0 };
      shapes.push(Box::new(circle));
  
      let area = compute_area(shapes);
      println!(&quot;Area {:?}&quot;, area);
  }
  </code></pre>
  <p>Here's the error report:</p>
  <pre><code>error[E0038]: the trait `Shape` cannot be made into an object
    --&gt; visitor.rs:26:25
     |
  26 | fn compute_area(shapes: Vec&lt;Box&lt;dyn Shape&gt;&gt;) -&gt; f64 {
     |                         ^^^^^^^^^^^^^^^^^^^ `Shape` cannot be made into an object
     |
     = help: consider moving `accept` to another trait
  note: for a trait to be &quot;object safe&quot; it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit &lt;https://doc.rust-lang.org/reference/items/traits.html#object-safety&gt;
    --&gt; visitor.rs:11:8
     |
  10 | trait Shape {
     |       ----- this trait cannot be made into an object...
  11 |     fn accept&lt;V: ShapeVisitor&gt;(&amp;self, sv: &amp;mut V);
     |        ^^^^^^ ...because method `accept` has generic type parameters
  
  error: aborting due to previous error
  </code></pre>
  <p>Is there a simple way to rectify it?</p>