pub struct ReadWriteMmap<'a> { /* private fields */ }
Expand description
A read-write memory mapping that allows shared access to a file’s contents.
This struct provides a safe wrapper around platform-specific memory mapping implementations, allowing read and write access to a file’s contents through memory mapping. The mapping can be created with a specific offset and length.
The mapping cannot outlive the file handle it was created from.
Implementations§
Source§impl<'a> ReadWriteMmap<'a>
impl<'a> ReadWriteMmap<'a>
Sourcepub fn new(
handle: &'a ReadWriteFileHandle,
offset: u64,
len: usize,
) -> Result<Self, MmapError>
pub fn new( handle: &'a ReadWriteFileHandle, offset: u64, len: usize, ) -> Result<Self, MmapError>
Creates a new read-write memory mapping for the specified file handle.
§Arguments
handle
- The file handle to create the mapping fromoffset
- The offset into the file where the mapping should beginlen
- The length of the mapping in bytes
§Errors
Returns a MmapError
if:
- The mapping cannot be created
- The length is zero
- The offset and length would exceed the file size
- The system cannot allocate the required resources
Sourcepub fn as_slice(&self) -> &'a [u8] ⓘ
pub fn as_slice(&self) -> &'a [u8] ⓘ
Returns a slice of the mapped memory. The lifetime of the slice is the same as the mapping.
Sourcepub fn as_mut_slice(&mut self) -> &'a mut [u8] ⓘ
pub fn as_mut_slice(&mut self) -> &'a mut [u8] ⓘ
Returns a mutable slice of the mapped memory. The lifetime of the slice is the same as the mapping.
Sourcepub fn data(&self) -> *mut u8
pub fn data(&self) -> *mut u8
Returns a raw pointer to the mapped memory.
The returned pointer is adjusted for the requested offset, accounting for any page alignment requirements.
§Safety
The caller must ensure that the memory is accessed within the bounds of
the mapping. The caller must also ensure the pointer does not outlast the
lifetime of the mapping. It is recommended you use Self::as_slice
or
Self::as_mut_slice
instead for compiler enforced safety.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the mapped region in bytes.
This returns the originally requested length, not including any padding added for page alignment.
Sourcepub fn advise(&self, advice: MemoryAdvice)
pub fn advise(&self, advice: MemoryAdvice)
Provides advice to the operating system about how the memory mapping will be accessed.
§Arguments
advice
- Bit flags indicating the expected access patterns for this memory region
§Note
This is a hint to the operating system and may be ignored. Not all advice types
are supported on all platforms. On Windows, only MemoryAdvice::WILL_NEED
has an effect.
Multiple advice flags can be combined using bitwise operations.
Trait Implementations§
Source§impl<'a> Clone for ReadWriteMmap<'a>
impl<'a> Clone for ReadWriteMmap<'a>
Source§fn clone(&self) -> ReadWriteMmap<'a>
fn clone(&self) -> ReadWriteMmap<'a>
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more