As written, this code works but the commented out line gives error[E0576]: cannot find method or associated constant meow in trait Foo.
pub trait Foo { }
struct Bar;
impl Foo {
pub fn meow() { println!("Hello World!") }
}
impl Foo for Bar { }
fn main() {
Foo::meow();
// <Bar as Foo>::meow();
}
If we want an inherent-like method meow for every type that satisfying Foo, but do not want clients modifying it, then we need a helper trait.
pub trait ImplFoo {
fn meow() { println!("Bye World!") }
}
impl<T> ImplFoo for T where T: Foo { }
fn main() {
Bar::meow();
}
Worse, we need new help traits for every inherent-like block we want to create, so our API gets polluted with several exposed helper traits. Also this Foo::meow() looks kinda iffy.
I'd think inherent impls could simply "work as expected" for traits, so all three call sites would call the original :
impl Foo {
pub fn meow() { println!("Hello World!") }
}
For visibility purposes, these inherent trait methods could be treated as methods on an invisible wrapper struct that has the same name as the trait Foo:
struct Foo<T: Foo>(T);
impl<T: Foo> Foo<T> {
pub fn meow() { println!("Hello World!") }
}
In this way, if Foo is defined in module A and meow in module B then the visibility of meow can be controlled as you would control the visibility of B::Foo.
As written, this code works but the commented out line gives error[E0576]: cannot find method or associated constant
meowin traitFoo.If we want an inherent-like method
meowfor every type that satisfyingFoo, but do not want clients modifying it, then we need a helper trait.Worse, we need new help traits for every inherent-like block we want to create, so our API gets polluted with several exposed helper traits. Also this
Foo::meow()looks kinda iffy.I'd think inherent
impls could simply "work as expected" for traits, so all three call sites would call the original :For visibility purposes, these inherent trait methods could be treated as methods on an invisible wrapper
structthat has the same name as the traitFoo:In this way, if
Foois defined in moduleAandmeowin moduleBthen the visibility ofmeowcan be controlled as you would control the visibility ofB::Foo.