Skip to content

Commit 2d62d01

Browse files
committed
Tidy file stream implementation, bump major version.
1 parent 72877a9 commit 2d62d01

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "binary_rw"
3-
version = "3.0.0"
3+
version = "4.0.0"
44
authors = ["Mathias Danielsen <mathiasda98@hotmail.com>"]
55
edition = "2021"
66

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod error;
1616
mod stream;
1717

1818
pub use error::BinaryError;
19-
pub use stream::file::{FileStream, OpenType};
19+
pub use stream::file::FileStream;
2020
pub use stream::memory::MemoryStream;
2121
pub use stream::slice::SliceStream;
2222

src/stream/file.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,69 @@ use std::io::prelude::*;
55
use std::io::{Error, ErrorKind, Read, SeekFrom, Write};
66
use 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

2711
impl 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

3936
impl 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

5350
impl 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

6562
impl 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

7572
impl ReadStream for FileStream {}
7673
impl WriteStream for FileStream {}
77-
78-
impl From<File> for FileStream {
79-
fn from(file: File) -> Self {
80-
Self { file }
81-
}
82-
}

tests/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use anyhow::Result;
22
use binary_rw::{
3-
BinaryReader, BinaryWriter, Endian, FileStream, MemoryStream, OpenType, SeekStream, SliceStream,
3+
BinaryReader, BinaryWriter, Endian, FileStream, MemoryStream, SeekStream, SliceStream,
44
};
55

66
fn create_writer_stream(name: &str) -> FileStream {
77
let name = format!("{}.test", name);
8-
FileStream::new(&name, OpenType::OpenAndCreate).expect("Failed to open stream")
8+
FileStream::create(&name).expect("Failed to open stream")
99
}
1010

1111
fn create_reader_stream(name: &str) -> FileStream {
1212
let name = format!("{}.test", name);
13-
FileStream::new(&name, OpenType::Open).expect("Failed to open stream")
13+
FileStream::open(&name).expect("Failed to open stream")
1414
}
1515

1616
fn cleanup(name: &str) {

0 commit comments

Comments
 (0)