[−][src]Struct vmap::Map
Allocation of one or more read-only sequential pages.
Example
use vmap::{Map, AdviseAccess, AdviseUsage}; use std::fs::OpenOptions; let file = OpenOptions::new().read(true).open("README.md")?; let page = Map::file(&file, 39, 30)?; page.advise(AdviseAccess::Sequential, AdviseUsage::WillNeed)?; assert_eq!(b"fast and safe memory-mapped IO", &page[..]); assert_eq!(b"safe", &page[9..13]);
Methods
impl Map
[src]
pub fn file(f: &File, offset: usize, length: usize) -> Result<Self>
[src]
Create a new map object from a range of a file.
Example
use std::fs::OpenOptions; use vmap::Map; let file = OpenOptions::new().read(true).open("README.md")?; let map = Map::file(&file, 0, 69)?; assert_eq!(map.is_empty(), false); assert_eq!(b"fast and safe memory-mapped IO", &map[39..69]); let map = Map::file(&file, 0, file.metadata()?.len() as usize + 1); assert!(map.is_err());
pub unsafe fn file_unchecked(
f: &File,
offset: usize,
length: usize
) -> Result<Self>
[src]
f: &File,
offset: usize,
length: usize
) -> Result<Self>
Create a new map object from a range of a file without bounds checking.
Safety
This does not verify that the requsted range is valid for the file. This can be useful in a few scenarios:
- When the range is already known to be valid.
- When a valid sub-range is known and not exceeded.
- When the range will become valid and is not used until then.
Example
use std::fs::OpenOptions; use vmap::Map; let file = OpenOptions::new().read(true).open("README.md")?; let map = unsafe { Map::file_unchecked(&file, 0, file.metadata()?.len() as usize + 1)? }; // It is safe read the valid range of the file. assert_eq!(b"fast and safe memory-mapped IO", &map[39..69]);
pub unsafe fn from_ptr(ptr: *mut u8, len: usize) -> Self
[src]
Constructs a new mutable map object from an existing mapped pointer.
Safety
This does not know or care if ptr
or len
are valid. That is,
it may be null, not at a proper page boundary, point to a size
different from len
, or worse yet, point to a properly mapped
pointer from some other allocation system.
Generally don't use this unless you are entirely sure you are doing so correctly.
Example
use vmap::{Map, Protect}; use std::fs::OpenOptions; let file = OpenOptions::new().read(true).open("src/lib.rs")?; let page = unsafe { let len = vmap::allocation_size(); let ptr = vmap::os::map_file(&file, 0, len, Protect::ReadOnly)?; Map::from_ptr(ptr, len) }; assert_eq!(b"fast and safe memory-mapped IO", &page[33..63]);
pub fn make_mut(self) -> Result<MapMut>
[src]
Transfer ownership of the map into a mutable map.
This will change the protection of the mapping. If the original file was not opened with write permissions, this will error.
Example
use vmap::Map; use std::io::Write; use std::fs::OpenOptions; use std::path::PathBuf; let path: PathBuf = /* path to file */ let file = OpenOptions::new().read(true).write(true).open(&path)?; // Map the beginning of the file let map = Map::file(&file, 0, 14)?; assert_eq!(b"this is a test", &map[..]); let mut map = map.make_mut()?; { let mut data = &mut map[..]; data.write_all(b"that")?; } assert_eq!(b"that is a test", &map[..]);
pub fn len(&self) -> usize
[src]
Get the length of the allocated region.
pub fn as_ptr(&self) -> *const u8
[src]
Get the pointer to the start of the allocated region.
pub fn advise(&self, access: AdviseAccess, usage: AdviseUsage) -> Result<()>
[src]
Updates the advise for the entire mapped region..
pub fn advise_range(
&self,
off: usize,
len: usize,
access: AdviseAccess,
usage: AdviseUsage
) -> Result<()>
[src]
&self,
off: usize,
len: usize,
access: AdviseAccess,
usage: AdviseUsage
) -> Result<()>
Updates the advise for a specific range of the mapped region.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,