MemfdOptions

Struct MemfdOptions 

Source
pub struct MemfdOptions { /* private fields */ }
Available on Android or Linux only.
Expand description

A Memfd builder, providing advanced options and flags for specifying its behavior.

Implementations§

Source§

impl MemfdOptions

Source

pub const fn new() -> Self

Default set of options for Memfd creation.

The default options are:

  • FileSeal::SealSeal (i.e. no further sealing);
  • close-on-exec is enabled;
  • hugetlb is disabled.
Source

pub const fn allow_sealing(self, value: bool) -> Self

Whether to allow adding seals to the created Memfd.

Examples found in repository?
examples/sized.rs (line 14)
12fn main() {
13    // Create a sealable memfd.
14    let opts = memfd::MemfdOptions::default().allow_sealing(true);
15    let mfd = opts.create("sized-1K").unwrap();
16
17    // Resize to 1024B.
18    mfd.as_file().set_len(1024).unwrap();
19
20    // Add seals to prevent further resizing.
21    mfd.add_seals(&[
22        memfd::FileSeal::SealShrink,
23        memfd::FileSeal::SealGrow
24    ]).unwrap();
25
26    // Prevent further sealing changes.
27    mfd.add_seal(memfd::FileSeal::SealSeal).unwrap();
28
29    // Write 1K of data, allowed by size seals.
30    let data_1k = vec![0x00; 1024];
31    let r = mfd.as_file().write_all(&data_1k);
32    assert!(r.is_ok());
33    mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
34
35    // Write 2K of data, now allowed by size seals.
36    let data_2k = vec![0x11; 2048];
37    let r = mfd.as_file().write_all(&data_2k);
38    assert!(r.is_err());
39    mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
40
41    // Try to resize to 2048B, not allowed by size seals.
42    let r = mfd.as_file().set_len(2048);
43    assert!(r.is_err());
44
45    // Overwrite 1K of data, allowed by size seals.
46    let data_1k = vec![0x22; 1024];
47    let r = mfd.as_file().write_all(&data_1k);
48    assert!(r.is_ok());
49}
Source

pub const fn close_on_exec(self, value: bool) -> Self

Whether to set the FD_CLOEXEC flag on the created Memfd.

Source

pub const fn hugetlb(self, size: Option<HugetlbSize>) -> Self

Optional hugetlb support and page size for the created Memfd.

Source

pub fn create<T: AsRef<str>>(&self, name: T) -> Result<Memfd, Error>

Create a Memfd according to configuration.

Examples found in repository?
examples/sized.rs (line 15)
12fn main() {
13    // Create a sealable memfd.
14    let opts = memfd::MemfdOptions::default().allow_sealing(true);
15    let mfd = opts.create("sized-1K").unwrap();
16
17    // Resize to 1024B.
18    mfd.as_file().set_len(1024).unwrap();
19
20    // Add seals to prevent further resizing.
21    mfd.add_seals(&[
22        memfd::FileSeal::SealShrink,
23        memfd::FileSeal::SealGrow
24    ]).unwrap();
25
26    // Prevent further sealing changes.
27    mfd.add_seal(memfd::FileSeal::SealSeal).unwrap();
28
29    // Write 1K of data, allowed by size seals.
30    let data_1k = vec![0x00; 1024];
31    let r = mfd.as_file().write_all(&data_1k);
32    assert!(r.is_ok());
33    mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
34
35    // Write 2K of data, now allowed by size seals.
36    let data_2k = vec![0x11; 2048];
37    let r = mfd.as_file().write_all(&data_2k);
38    assert!(r.is_err());
39    mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
40
41    // Try to resize to 2048B, not allowed by size seals.
42    let r = mfd.as_file().set_len(2048);
43    assert!(r.is_err());
44
45    // Overwrite 1K of data, allowed by size seals.
46    let data_1k = vec![0x22; 1024];
47    let r = mfd.as_file().write_all(&data_1k);
48    assert!(r.is_ok());
49}

Trait Implementations§

Source§

impl Clone for MemfdOptions

Source§

fn clone(&self) -> MemfdOptions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MemfdOptions

Source§

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

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

impl Default for MemfdOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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

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.