pub struct Fork { /* private fields */ }Expand description
A Spawner-like structure that executes a closure instead of another process. Specifically,
it forks the current caller, runs the closure within the child, then serializes and returns
the result to the parent via a pipe.
Your return type, if one exists, must implement Serialize + Deserialize from serde, as the
closure is run under a separate process (the child). This means that “returning” the result
requires serializing the result and sending it back to the parent. This operates identically
to how standard Input/Output/Error is captured in Spawn.
Implementations§
Source§impl Fork
impl Fork
Sourcepub fn caps(self, caps: impl IntoIterator<Item = Capability>) -> Self
pub fn caps(self, caps: impl IntoIterator<Item = Capability>) -> Self
See Spawner::caps
Sourcepub fn new_privileges(self, allow: bool) -> Self
pub fn new_privileges(self, allow: bool) -> Self
See Spawner::new_privileges
Sourcepub fn caps_i(&mut self, caps: impl IntoIterator<Item = Capability>)
pub fn caps_i(&mut self, caps: impl IntoIterator<Item = Capability>)
See Spawner::caps_i
Sourcepub fn new_privileges_i(self, allow: bool)
pub fn new_privileges_i(self, allow: bool)
See Spawner::new_privileges_i
Sourcepub unsafe fn fork<F, R>(self, op: F) -> Result<R, Error>
pub unsafe fn fork<F, R>(self, op: F) -> Result<R, Error>
Run a closure within a fork.
§Example
let result = unsafe { spawn::Fork::new().fork(|| 1) }.unwrap();
assert!(result == 1);§Safety
This function does not call execve. The closure runs within the fork, which has several considerations:
- The code should not make allocations. Though the default memory allocator on Linux often works in such an environment, it should not be relied upon.
- If you had a signal handler installed, this tries and drops all of them. Other such primitives may fail at any point, and should not be relied upon.
If your closure returns a value, it must implement Serialize, as the closure is running under a separate process, and must be transmitted to the parent through a pipe.
Sourcepub unsafe fn fork_fd<F, R>(self, op: F) -> Result<OwnedFd, Error>
pub unsafe fn fork_fd<F, R>(self, op: F) -> Result<OwnedFd, Error>
This is a specialized version of fork() that uses the SCM-Rights of a Unix Socket to transmit a FD to the parent. This could be used to open a file under one operating mode, and send the FD to the parent under another operating mode.
§Safety
See fork()
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Fork
impl !RefUnwindSafe for Fork
impl Send for Fork
impl Sync for Fork
impl Unpin for Fork
impl !UnwindSafe for Fork
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more