Struct shmem_bind::Builder

source ·
pub struct Builder { /* private fields */ }

Implementations§

source§

impl Builder

source

pub fn new(id: &str) -> Self

Examples found in repository?
examples/message-passing.rs (line 26)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
fn main() -> Result<(), Box<dyn Error>> {
    // create new shared memory pointer with desired size
    //
    // first call to this function with the same FILE_LINK_ID would result in creating a new shared
    // memory file and owning it. this would result in deleting the shared memory when the variable
    // goes out of scope.
    // the second call to this function will only open shared memory and would not delete it.
    let shared_mem = shmem::Builder::new("shmem-example_message-passing.shm")
        .with_size(mem::size_of::<Message>() as i64)
        .open()?;

    // wrap the raw shared memory ptr with desired Boxed type
    // user must ensure that the data the pointer is pointing to is initialized and valid for use
    let mut message = unsafe { shared_mem.boxed::<Message>() };

    let mut args = std::env::args();
    let num_args = args.len();
    match num_args {
        // parent process
        1 => {
            // ensure that first process owns the shared memory (used for cleanup)
            let mut message = ShmemBox::own(message);

            // initiate the data behind the boxed pointer
            message.val = 1;

            let binary_path = args.next().unwrap();
            let new_val = 5;
            // create new process to mutate the shared memory
            let mut handle = Command::new(&binary_path)
                .arg(format!("{new_val}"))
                .spawn()
                .unwrap();
            handle.wait()?;

            // assert that the new process mutated the shared memory
            assert_eq!(message.val, new_val);

            // message is dropped here, shared memory IS deallocated
        }
        // child process
        2 => {
            let value = std::env::args().last().unwrap().parse()?;

            message.val = value;

            // message is dropped here, shared memory IS NOT deallocated
        }
        _ => unimplemented!(),
    }
    Ok(())
}
source

pub fn with_size(self, size: i64) -> BuilderWithSize

Examples found in repository?
examples/message-passing.rs (line 27)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
fn main() -> Result<(), Box<dyn Error>> {
    // create new shared memory pointer with desired size
    //
    // first call to this function with the same FILE_LINK_ID would result in creating a new shared
    // memory file and owning it. this would result in deleting the shared memory when the variable
    // goes out of scope.
    // the second call to this function will only open shared memory and would not delete it.
    let shared_mem = shmem::Builder::new("shmem-example_message-passing.shm")
        .with_size(mem::size_of::<Message>() as i64)
        .open()?;

    // wrap the raw shared memory ptr with desired Boxed type
    // user must ensure that the data the pointer is pointing to is initialized and valid for use
    let mut message = unsafe { shared_mem.boxed::<Message>() };

    let mut args = std::env::args();
    let num_args = args.len();
    match num_args {
        // parent process
        1 => {
            // ensure that first process owns the shared memory (used for cleanup)
            let mut message = ShmemBox::own(message);

            // initiate the data behind the boxed pointer
            message.val = 1;

            let binary_path = args.next().unwrap();
            let new_val = 5;
            // create new process to mutate the shared memory
            let mut handle = Command::new(&binary_path)
                .arg(format!("{new_val}"))
                .spawn()
                .unwrap();
            handle.wait()?;

            // assert that the new process mutated the shared memory
            assert_eq!(message.val, new_val);

            // message is dropped here, shared memory IS deallocated
        }
        // child process
        2 => {
            let value = std::env::args().last().unwrap().parse()?;

            message.val = value;

            // message is dropped here, shared memory IS NOT deallocated
        }
        _ => unimplemented!(),
    }
    Ok(())
}

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

§

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

§

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.