Struct winsafe::File

source ·
pub struct File { /* private fields */ }
Available on crate feature kernel only.
Expand description

Manages an HFILE handle, which provides file read/write and other operations. It is closed automatically when the object goes out of scope.

This is an alternative to the standard std::fs::File, with a possibly faster implementation since it’s Windows-only.

If you just want to read the file, consider memory-mapping it with FileMapped, which tends to be faster.

§Examples

Reading the contents as a string:

use winsafe::{self as w, prelude::*};

let f = w::File::open(
    "C:\\Temp\\foo.txt",
    w::FileAccess::ExistingRW,
)?;
let raw_bytes = f.read_all()?;
let text = w::WString::parse(&raw_bytes)?.to_string();

Erasing the file and writing a string:

use winsafe::{self as w, prelude::*};

let f = w::File::open(
    "C:\\Temp\\foo.txt",
    w::FileAccess::OpenOrCreateRW,
)?;
f.set_size(0)?; // truncate
f.write("My text".as_bytes())?;

Implementations§

source§

impl File

source

pub fn open(file_path: &str, access: FileAccess) -> SysResult<Self>

Opens a file with the desired access.

source

pub fn hfile(&self) -> &HFILE

Returns the underlying file handle.

source

pub fn pointer_offset(&self) -> SysResult<u64>

Returns the position of the file pointer by calling HFILE::SetFilePointerEx.

source

pub fn read(&self, buffer: &mut [u8]) -> SysResult<u32>

Calls HFILE::ReadFile to read at most buffer.len() bytes from the file, starting at the current file pointer offset. Returns how many bytes were actually read. The file pointer is then incremented by the number of bytes read.

Note that the API limits the reading up to 4 GB.

source

pub fn read_all(&self) -> SysResult<Vec<u8>>

Returns the size of the file by calling HFILE::GetFileSizeEx, allocates the Vec buffer, then reads all the file bytes by calling HFILE::ReadFile.

Note that the API limits the reading up to 4 GB.

source

pub fn set_pointer_offset(&self, offset: u64) -> SysResult<()>

Sets the position of the file pointer by calling HFILE::SetFilePointerEx.

source

pub fn set_size(&self, num_bytes: u64) -> SysResult<()>

Truncates or expands the file by calling HFILE::SetFilePointerEx and HFILE::SetEndOfFile, then sets the file pointer to the beginning of the file.

If the size is increased, the contents in the new area are undefined.

source

pub fn size(&self) -> SysResult<u64>

Returns the size of the file by calling HFILE::GetFileSizeEx.

source

pub fn times(&self) -> SysResult<(SYSTEMTIME, SYSTEMTIME)>

Returns the creation and last write times of the file, in the current time zone.

source

pub fn write(&self, data: &[u8]) -> SysResult<()>

Writes the bytes at the current file pointer by calling HFILE::WriteFile.

This method will fail if the file was opened with FileAccess::ExistingReadOnly.

Auto Trait Implementations§

§

impl Freeze for File

§

impl RefUnwindSafe for File

§

impl Send for File

§

impl !Sync for File

§

impl Unpin for File

§

impl UnwindSafe for File

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.