How to design libs properly?

31 points DrRuhe

I wonder if there is a collection of best practices for developing Libraries/Crates somewhere?

​

Currently, I am developing a library, and one of my principles is to provide traits. These traits provide default implementations for the hard but generalizable methods, so that users only need to define 1-2 trivial methods to use it. However since users can only implement traits on types from their crate, the use case of those traits is restricted to just a few types the user defined themselves.

​

I know that the user could wrap any type in a wrapper struct, but that just seems backwards and non-user-friendly.

​

As an example:

The user wants to use the &str type with my lib, but since they did not define &str themselves, they cannot implement traits of my lib to help them out.