#Visibility of test utils

7 messages · Page 1 of 1 (latest)

dense zephyr
#

In some crates I need to use some test utilities I've created. Recently I found use for those test utils in my documentation's examples.

As a concrete example, since the Rust API Guidelines recommends using ? in examples, I created an AnyError struct so that I can use ? on different results.

The problem is that this AnyError struct is not meant to be public, it is just a test utility for this kind of cases, yet documentation tests are tested in an environment that is not flagged as test (so no #[cfg(test)] possible on the test_utils module), and seems to be run in an external environment (so pub(crate) won't cut it either) .

Should I really put those test utilities in public? or how would you solve this issue?

PS: I want to keep my dependencies at a minimum, so no dependencies on libs that do something similar to my AnyError struct

grave coral
#

Can you use Box<dyn Error>?

dense zephyr
# grave coral Can you use `Box<dyn Error>`?

Yes, that's what I use internally for AnyError, but then you also have the generic From implementation so that ? works on any result.
I used to just inline the declaration of AnyError and this conversion in the documentation examples, as hidden lines, but I just want it to be simpler and take less space.

grave coral
#

Box<dyn Error> already has a From impl. If you put it as the return type of the doc function it should work.

dense zephyr
grave coral
#

It's more obvious in Error's impls but still kinda hard to find without knowing about it beforehand.

delicate idol
#

in general for things that need to be public for reasons like use in doctests or benchmarks, the solution is to make them public and #[doc(hidden)]