pub struct Context { /* private fields */ }Expand description
The execution context for a process.
A Context is passed to each process and provides access to:
- The process’s own PID
- The process’s mailbox for receiving messages
- Methods for sending messages to other processes
- Methods for creating links and monitors
§Examples
async fn my_process(ctx: Context) {
let my_pid = ctx.pid();
// Receive a message
if let Some(envelope) = ctx.recv().await {
// Handle message
}
// Send a message to another process
ctx.send(other_pid, &MyMessage { data: 42 }).unwrap();
}Implementations§
Source§impl Context
impl Context
Sourcepub fn new(
pid: Pid,
mailbox: Mailbox,
state: Arc<RwLock<ProcessState>>,
registry: ProcessRegistry,
) -> Self
pub fn new( pid: Pid, mailbox: Mailbox, state: Arc<RwLock<ProcessState>>, registry: ProcessRegistry, ) -> Self
Creates a new context for a process.
Sourcepub async fn recv(&mut self) -> Option<Vec<u8>>
pub async fn recv(&mut self) -> Option<Vec<u8>>
Receives the next message from the mailbox.
Returns None if the mailbox is closed.
Sourcepub async fn recv_timeout(
&mut self,
timeout: Duration,
) -> Result<Option<Vec<u8>>, ()>
pub async fn recv_timeout( &mut self, timeout: Duration, ) -> Result<Option<Vec<u8>>, ()>
Receives the next message with a timeout.
Returns Ok(Some(data)) if a message was received,
Ok(None) if the mailbox was closed,
or Err(()) if the timeout elapsed.
Sourcepub fn send_raw(&self, pid: Pid, data: Vec<u8>) -> Result<(), SendError>
pub fn send_raw(&self, pid: Pid, data: Vec<u8>) -> Result<(), SendError>
Sends a raw message to another process.
Sourcepub fn send<M: Term>(&self, pid: Pid, msg: &M) -> Result<(), SendError>
pub fn send<M: Term>(&self, pid: Pid, msg: &M) -> Result<(), SendError>
Sends a typed message to another process.
Sourcepub fn set_trap_exit(&self, trap: bool) -> bool
pub fn set_trap_exit(&self, trap: bool) -> bool
Sets the trap_exit flag for this process.
When true, exit signals from linked processes are delivered
as SystemMessage::Exit messages instead of terminating this process.
Returns the previous value.
Sourcepub fn is_trapping_exits(&self) -> bool
pub fn is_trapping_exits(&self) -> bool
Returns whether this process is trapping exits.
Sourcepub fn link(&self, other: Pid) -> Result<(), SendError>
pub fn link(&self, other: Pid) -> Result<(), SendError>
Creates a bidirectional link with another process.
If either process terminates abnormally, the other will receive an exit signal.
Sourcepub fn monitor(&self, target: Pid) -> Result<Ref, SendError>
pub fn monitor(&self, target: Pid) -> Result<Ref, SendError>
Creates a monitor on another process.
Returns a reference that will be included in the DOWN message
when the monitored process terminates.
Sourcepub fn demonitor(&self, reference: Ref)
pub fn demonitor(&self, reference: Ref)
Removes a monitor.
The reference will no longer be valid and no DOWN message
will be sent for this monitor.
Sourcepub fn exit(&self, target: Pid, reason: ExitReason) -> Result<(), SendError>
pub fn exit(&self, target: Pid, reason: ExitReason) -> Result<(), SendError>
Sends an exit signal to another process.
If reason is ExitReason::Killed, the target will terminate
unconditionally. Otherwise, the behavior depends on whether
the target is trapping exits.
Sourcepub fn register(&self, name: String) -> bool
pub fn register(&self, name: String) -> bool
Registers a name for this process.
Returns false if the name is already taken.
Sourcepub fn unregister(&self, name: &str) -> Option<Pid>
pub fn unregister(&self, name: &str) -> Option<Pid>
Unregisters a name.