impl Pancake {

  fn as_circle(&self) -> &Circle { &self.circle }

  fn as_mut_circle(&mut self) -> &mut Circle { &mut self.circle }

}




The compiler will optimize trivial functions, except cross-crate. In those 
cases, use an #[inline] annotation.

On Thu, Oct 16, 2014 at 10:57 PM, David Henningsson <di...@ubuntu.com>
wrote:

> This is probably a previously asked question, but I couldn't find it on 
> Google, so...
> Let's extend the Circle example from the guide a little:
> struct  Circle  {
>      x:f64,
>      y:f64,
>      radius:f64,
> }
> trait  HasArea  {
>      fn  area(&self)->  f64;
> }
> impl  HasArea  for  Circle  {
>      fn  area(&self)->  f64  {
>          std::f64::consts::PI  *  (self.radius  *  self.radius)
>      }
> }
> struct Pancake {
>      circle: Circle,
>      is_tasty: bool,
> }
> ...now, what is the easiest way I can implement HasArea for Pancake? I 
> could do this:
> impl HasArea for Pancake {
> fn area(&self) -> f64 { self.circle.area() }
> }
> ...but that means a lot of boiler-plate code, especially if HasArea has 
> many methods. Hopefully rust will just inline/optimise the redirection 
> away in most cases to avoid the runtime cost, but is there a smarter or 
> more idiomatic way of doing this?
> // David
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to