@@ -5,78 +5,69 @@ use std::io::prelude::*;
55use std:: io:: { Error , ErrorKind , Read , SeekFrom , Write } ;
66use std:: path:: Path ;
77
8- /// Indicates how the file stream should open the underlying file.
9- pub enum OpenType {
10- /// Open and create the file if it does not exist.
11- ///
12- /// This will truncate the file if it already exists.
13- OpenAndCreate ,
14-
15- /// Open the file for reading and writing.
16- ReadWrite ,
17-
18- /// Open the file for reading.
19- Open ,
20- }
21-
228/// Stream that wraps a file.
23- pub struct FileStream {
24- file : File ,
25- }
9+ pub struct FileStream ( File ) ;
2610
2711impl FileStream {
2812 /// Create a file stream.
29- pub fn new < P : AsRef < Path > > ( path : P , open_type : OpenType ) -> Result < FileStream > {
30- let file = match open_type {
31- OpenType :: OpenAndCreate => File :: create ( path) ?,
32- OpenType :: ReadWrite => OpenOptions :: new ( ) . read ( true ) . write ( true ) . open ( path) ?,
33- OpenType :: Open => File :: open ( path) ?,
34- } ;
35- Ok ( Self { file } )
13+ pub fn new ( file : File ) -> Self {
14+ Self ( file)
15+ }
16+
17+ /// Create a file stream in write-only mode.
18+ ///
19+ /// If the file exists it is truncated, if it does not
20+ /// exist it will be created.
21+ pub fn create < P : AsRef < Path > > ( path : P ) -> Result < Self > {
22+ Ok ( Self ( File :: create ( path. as_ref ( ) ) ?) )
23+ }
24+
25+ /// Attempts to open a file stream in read-only mode.
26+ pub fn open < P : AsRef < Path > > ( path : P ) -> Result < Self > {
27+ Ok ( Self ( File :: open ( path. as_ref ( ) ) ?) )
28+ }
29+
30+ /// Attempts to open a file stream with read and write modes enabled.
31+ pub fn write < P : AsRef < Path > > ( path : P ) -> Result < Self > {
32+ Ok ( Self ( OpenOptions :: new ( ) . read ( true ) . write ( true ) . open ( path) ?) )
3633 }
3734}
3835
3936impl SeekStream for FileStream {
4037 fn seek ( & mut self , to : usize ) -> Result < usize > {
41- Ok ( self . file . seek ( SeekFrom :: Start ( to as u64 ) ) ? as usize )
38+ Ok ( self . 0 . seek ( SeekFrom :: Start ( to as u64 ) ) ? as usize )
4239 }
4340
4441 fn tell ( & mut self ) -> Result < usize > {
45- Ok ( self . file . seek ( SeekFrom :: Current ( 0 ) ) ? as usize )
42+ Ok ( self . 0 . seek ( SeekFrom :: Current ( 0 ) ) ? as usize )
4643 }
4744
4845 fn len ( & self ) -> Result < usize > {
49- Ok ( self . file . metadata ( ) ?. len ( ) . try_into ( ) ?)
46+ Ok ( self . 0 . metadata ( ) ?. len ( ) . try_into ( ) ?)
5047 }
5148}
5249
5350impl Read for FileStream {
5451 fn read ( & mut self , buffer : & mut [ u8 ] ) -> std:: io:: Result < usize > {
55- if self . tell ( ) . unwrap ( ) + buffer. len ( ) > self . file . metadata ( ) ?. len ( ) as usize {
52+ if self . tell ( ) . unwrap ( ) + buffer. len ( ) > self . 0 . metadata ( ) ?. len ( ) as usize {
5653 return Err ( Error :: new (
5754 ErrorKind :: UnexpectedEof ,
5855 BinaryError :: ReadPastEof ,
5956 ) ) ;
6057 }
61- Ok ( self . file . read ( buffer) ?)
58+ Ok ( self . 0 . read ( buffer) ?)
6259 }
6360}
6461
6562impl Write for FileStream {
6663 fn write ( & mut self , bytes : & [ u8 ] ) -> std:: io:: Result < usize > {
67- self . file . write ( bytes)
64+ self . 0 . write ( bytes)
6865 }
6966
7067 fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
71- self . file . flush ( )
68+ self . 0 . flush ( )
7269 }
7370}
7471
7572impl ReadStream for FileStream { }
7673impl WriteStream for FileStream { }
77-
78- impl From < File > for FileStream {
79- fn from ( file : File ) -> Self {
80- Self { file }
81- }
82- }
0 commit comments