Impl visibility


#1

I have a question regarding the visibility of some more specific impl blocks (just impl, not impl Trait).

Suppose library A defines type Foo<T>. Then there’s library B that defines type Bar and also an impl Foo<Bar> block that contains some Bar-specific member functions.

What are the visibility rules of these member functions? Does user code need to use the Bar type in order to have them in scope?


#2

Good question. Any methods inside impl Foo<Bar> are inherent methods, so you don’t need anything at all in scope to use them. You can have access to a Foo<Bar> value without having any of those in scope: Let’s pretend that self.xyz.foo is a Foo<Bar> value. Then you can call the inherent methods like self.xyz.foo.methodname().


#3

Really? Wow, cool. Thanks for answer :slight_smile:

I suppose given how UFCS works, this makes sense…


#4

Ok, to address the different crates part of the question, the B crate part is not allowed. It can’t add inherent methods to Foo. So for it to happen, all the inherent methods would have to be in the first crate.


#5

Ah, ok, that’s a bit of a bummer. Glad it’s cleared up for me though…