inherent-pub 0.1.0

Mark methods in `impl Trait for Type` blocks inherent to the type
Documentation

Build Status Latest Version Rust Documentation

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:

mod geometry {
    pub trait Length {
        fn length(&self) -> f64;
    }

    pub struct Vector(pub f64, pub f64);

    impl Length for Vector {
        fn length(&self) -> f64 {
            let Vector(x, y) = self;
            (x.powi(2) + y.powi(2)).sqrt()
        }
    }
}

fn main() {
	// Compilation error: we did not `use geometry::Length` so we can't access `length()`
    assert!(geometry::Vector(3.0, 4.0).length() == 5.0);
}

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:

extern crate inherent_pub;

mod geometry {
    use inherent_pub::inherent_pub;

    pub trait Length {
        fn length(&self) -> f64;
    }

    pub struct Vector(pub f64, pub f64);

    #[inherent_pub]
    impl Length for Vector {
        pub fn length(&self) -> f64 {
            let Vector(x, y) = self;
            (x.powi(2) + y.powi(2)).sqrt()
        }
    }
}

fn main() {
    assert!(geometry::Vector(3.0, 4.0).length() == 5.0);
}