pre-RFC: Automatic generic-to-dynamic conversion

Come to think of it, I’ve written some very large functions that take some <P: AsRef<Path>> or <R: Read> which are entirely finished using said object by the end of the first statement. Maybe we can solve that problem instead.

I.e. use some heuristic to transform this:

pub fn read<R: Read>(r: R) -> Result<Config> {
    let raw = ::serde_yaml::from_reader(r)?;

    // ...lots of code...
}

into this:

pub fn read<R: Read>(r: R) -> Result<Config> {
    // this gets compiled once
    fn _impl(a: ::serde_yaml::Result<RawConfig>) -> Result<Config> {
        // ...lots of code...
    }
    // this gets compiled many times
    _impl(::serde_yaml::from_reader(r))
}
12 Likes