Skip to main content

Mmap

Struct Mmap 

Source
pub struct Mmap { /* private fields */ }
Expand description

A read-only, private memory map of an open file.

The map is never written. It keeps the mapped pages alive on its own, so the caller can close the file descriptor after Mmap::read_only returns.

§Truncation

A read of a mapped page with no file bytes behind it raises SIGBUS. No safe API can expose this signal, so Mmap::read_only measures the file and refuses a map longer than the file. Mmap::as_slice is sound for the whole life of the map if no writer truncates the file. The caller must map only a file that no other writer can truncate.

Implementations§

Source§

impl Mmap

Source

pub fn read_only(fd: impl AsFd, len: usize) -> Result<Mmap>

Maps the first len bytes of fd read-only.

len must be larger than zero and not larger than the size of the file. Because the function refuses a len larger than the file, a read of the map cannot raise SIGBUS while the file keeps its size.

§Errors
  • An Errno from fstat if the function cannot read the size of the file.
  • EINVAL if len is larger than the size of the file.
  • EINVAL from mmap if len is zero.
  • Another Errno from mmap, for example EACCES if fd is not open for reading.
§Examples
use std::fs::File;

use ostrya_sys::Mmap;

let path = std::env::temp_dir().join(format!("ostrya-sys-doc-{}", std::process::id()));
std::fs::write(&path, b"ostree")?;
let file = File::open(&path)?;
assert!(Mmap::read_only(&file, 7).is_err(), "longer than the file");
let map = Mmap::read_only(&file, 6)?;
// The map stays valid after the descriptor closes.
drop(file);
assert_eq!(map.as_slice(), b"ostree");
std::fs::remove_file(&path)?;
Source

pub fn as_slice(&self) -> &[u8] ⓘ

Returns the mapped bytes.

Source

pub fn len(&self) -> usize

Returns the length of the map in bytes.

Source

pub fn is_empty(&self) -> bool

Returns true if the map covers zero bytes.

The result is always false, because Mmap::read_only refuses a length of zero.

Trait Implementations§

Source§

impl Debug for Mmap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Mmap

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Mmap

Source§

impl Sync for Mmap

Auto Trait Implementations§

§

impl Forget for Mmap

§

impl Freeze for Mmap
where *const u8 is !null: Freeze,

§

impl Move for Mmap

§

impl RefUnwindSafe for Mmap

§

impl Unpin for Mmap
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Mmap
where *const u8 is !null: UnsafeUnpin,

§

impl UnwindSafe for Mmap

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>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.