Rust Inherent Pub
Motivation
If you impl a trait on a type, you'll only be able to use the methods in that
impl block if you import the trait:
But what if the method is a natural member of the type itself, not just the
trait? Shouldn't we be able to just access the length() of any Vector
without having to explicitly tell Rust that we are using Length?
Enter the #[inherent_pub] procedural macro attribute!
Usage
With #[inherent_pub], we can make it so length() can be used without
importing the Length trait - just annotated the impl block and mark the
method as pub:
How the desugaring works?
#[inherent_pub] removes the pub from the methods and adds another impl
block with inherent methods that get redirected to the trait ones. So:
Gets desugared to:
Rather than handling special arguments like ignored arguments that start with
(or are) a single underscore _ or pattern arguments, #[inherent_pub] simply
replaces all arguments (other than self) with generic names. So:
Gets desugared to: