Can i implement trait for other library type?


#1

Hello,

Is it possible to implement my own trait for some third party library?


#2

Yes. The only trick is that the trait must be in scope (use Trait) when you use it.


#3

awesome. So, I can, for example, implement my own trait, for TcpStream in Tokio . right ?


#4

Yes. You can implement your own trait for not-your type, or you can implement not-your trait for your type. As long as one of the sides is defined in your crate, it’s fine.