Trait winsafe::prelude::kernel_Hfilemapview

source ·
pub trait kernel_Hfilemapview: Handle {
    // Provided methods
    fn as_mut_slice(&self, len: usize) -> &mut [u8]  { ... }
    fn as_slice(&self, len: usize) -> &[u8]  { ... }
    fn FlushViewOfFile(
        &self,
        start_at_byte: usize,
        num_bytes: usize
    ) -> SysResult<()> { ... }
}
Available on crate feature kernel only.
Expand description

This trait is enabled with the kernel feature, and provides methods for HFILEMAPVIEW.

Prefer importing this trait through the prelude:

use winsafe::prelude::*;

Provided Methods§

source

fn as_mut_slice(&self, len: usize) -> &mut [u8]

Returns a slice representing the mapped memory. You can modify the contents. You should call this method only if the file has write access.

Note: If the file is resized to a smaller size, the slice will still map the bytes beyond the file. This may cause serious errors. So, if the file is resized, re-generate the slice by calling as_slice again.

source

fn as_slice(&self, len: usize) -> &[u8]

Returns a slice representing the mapped memory.

Note: If the file is resized to a smaller size, the slice will still map the bytes beyond the file. This may cause serious errors. So, if the file is resized, re-generate the slice by calling as_slice again.

§Examples

Reading the contents of a file into a string:

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

let (hfile, _) = w::HFILE::CreateFile(
    "C:\\Temp\\test.txt",
    co::GENERIC::READ,
    Some(co::FILE_SHARE::READ),
    None,
    co::DISPOSITION::OPEN_EXISTING,
    co::FILE_ATTRIBUTE::NORMAL,
    None,
    None,
    None,
)?;

let hmap = hfile.CreateFileMapping(
    None,
    co::PAGE::READONLY,
    None,
    None,
)?;

let view = hmap.MapViewOfFile(co::FILE_MAP::READ, 0, None)?;

let slice = view.as_slice(hfile.GetFileSizeEx()? as _);
let text = std::str::from_utf8(slice)?;

println!("{}", text);
source

fn FlushViewOfFile( &self, start_at_byte: usize, num_bytes: usize ) -> SysResult<()>

FlushViewOfFile function.

Object Safety§

This trait is not object safe.

Implementors§