Say I have a function that looks something like this:
use std::path::Path;
fn do_complicated_thing(path: &Path) -> io::Result<()> {
// Does something really complicated
}
This function is nice, but if I wanted to provide an &str
for path
, I’d have to wrap it in Path::new()
, which isn’t very pretty. It’s much nicer to be able to just provide a string literal in some cases, and I can do that if I change the function signature:
fn do_complicated_thing<P: AsRef<Path>>(path: &P) -> ...
Now this function is relatively large, not the kind I would expect to get inlined into any call site if it’s used more than once. My concern is this: for each P
which implements AsRef<Path>
with which I invoke the function, will the compiler duplicate the entire function body, or will it be smart enough to “outline” the parts of the function which are independent of the generic parameter, and only specialize the parts which aren’t?