diff --git a/Cargo.toml b/Cargo.toml index 3977cba..6664a09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,4 @@ [package] name = "dropfile" -version = "1.0.0" +version = "1.0.1" edition = "2021" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 80f5581..b8dcc33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::{fs::{self, File}, io::{self, ErrorKind, IoSlice, Seek, Write}, ops::{Deref, DerefMut}, path::Path}; +use std::{fs::{self, File}, io::{self, ErrorKind, IoSlice, IoSliceMut, Seek, SeekFrom, Read, Write}, ops::{Deref, DerefMut}, path::Path}; pub struct DropFile { path: Box, @@ -34,14 +34,14 @@ impl DropFile { } pub fn trunc(&mut self) -> Result<(), &'static str> { - let file = self.file.as_mut().unwrap(); + let file = self.deref_mut(); file.rewind().map_err(|_| "failed to rewind file")?; file.set_len(0).map_err(|_| "failed to truncate file")?; return Ok(()); } pub fn trunc_to_cursor(&mut self) -> Result<(), &'static str> { - let file = self.file.as_mut().unwrap(); + let file = self.deref_mut(); let cursor = file.stream_position().map_err(|_| "failed to get cursor position")?; file.set_len(cursor).map_err(|_| "failed to truncate file")?; return Ok(()); @@ -70,6 +70,28 @@ impl DerefMut for DropFile { } } +impl Seek for DropFile { + fn seek(&mut self, pos: SeekFrom) -> io::Result { + return self.deref_mut().seek(pos); + } +} +impl Read for DropFile { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + return self.deref_mut().read(buf); + } + + fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result { + return self.deref_mut().read_vectored(bufs); + } + + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + return self.deref_mut().read_to_end(buf); + } + + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + return self.deref_mut().read_to_string(buf); + } +} impl Write for DropFile { fn write(&mut self, buf: &[u8]) -> io::Result { self.written_to = true;