Comment

Comment by nextaccountic

> If you do need generic interface for better type-safety and ergonomics, make sure that the interface layer is thin, and that it immediately delegates to a non-generic implementation. The classical example to internalize here are various functions from str::fs module which operate on paths:

    pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
      fn inner(path: &Path) -> io::Result<Vec<u8>> {
        let mut file = File::open(path)?;
        let mut bytes = Vec::new();
        file.read_to_end(&mut bytes)?;
        Ok(bytes)
      }
      inner(path.as_ref())
    }
--

Nobody seems to care, but there's a crate that does this for you https://github.com/llogiq/momo

But the Rust compiler should be doing this automatically :(

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton