Created
July 23, 2019 10:46
-
-
Save octave99/843d7aa9df7d5d54b7d3f9ead80f3e4f to your computer and use it in GitHub Desktop.
Store generic with trait bound
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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