Skip to content

Instantly share code, notes, and snippets.

@octave99
Created July 23, 2019 10:46
Show Gist options
  • Save octave99/843d7aa9df7d5d54b7d3f9ead80f3e4f to your computer and use it in GitHub Desktop.
Save octave99/843d7aa9df7d5d54b7d3f9ead80f3e4f to your computer and use it in GitHub Desktop.
Store generic with trait bound
use std::path::Path;
use std::io::Read;
pub struct AnimalFile<P> {
reader: P,
}
impl<P> AnimalFile<P>
where
P: NewBufRead<std::fs::File>,
{
pub fn new<F: AsRef<Path>>(fp: F) -> AnimalFile<P> {
let file = std::fs::OpenOptions::new().read(true).open(fp.as_ref()).unwrap();
Self {
reader: P::new_buf_read(file),
}
}
}
pub trait NewBufRead<R: Read>: std::io::BufRead {
fn new_buf_read(r: R) -> Self;
}
impl<R: Read> NewBufRead<R> for std::io::BufReader<R> {
fn new_buf_read(r: R) -> Self {
Self::new(r)
}
}
fn main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment