I understand that the trait's method doesn't have a body, so there is nothing to inline. But is there any sense to mark its default implementation like this?
trait Magnitude {
fn square_magnitude( &self ) -> f64;
#[inline]
fn magnitude( &self ) -> f64 {
self.square_magnitude().sqrt()
}
}
Do I need to rewrite whole method body and mark this impl's method with #[inline] when implementing the trait for type rather than marking just trait's method as above?
If I understand the question correctly, you are asking two things:
magnitude?square_magnitudeinsidemagnitudeifsquare_magnitudeis itself declaredinlineeven though the code forsquare_magnitudeis not available in the trait?As to the first, there is no reason why it couldn't. As to the second the answer is yes, the compiler will be able to inline both functions because by the time it generates the code, the source for both functions is available. This can be seen in the disassembly:
Compiled with rustc v1.28.0 and option
-O:Note however that the compiler will not inline
square_magnitudeinsidemagnitudeifsquare_magnitudeis not declaredinlineitself:Generates: