Skip to main content

Context

Struct Context 

Source
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

Source

pub fn new( pid: Pid, mailbox: Mailbox, state: Arc<RwLock<ProcessState>>, registry: ProcessRegistry, ) -> Self

Creates a new context for a process.

Source

pub fn pid(&self) -> Pid

Returns this process’s PID.

Source

pub async fn recv(&mut self) -> Option<Vec<u8>>

Receives the next message from the mailbox.

Returns None if the mailbox is closed.

Source

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.

Source

pub fn try_recv(&mut self) -> Option<Vec<u8>>

Tries to receive a message without blocking.

Source

pub fn send_raw(&self, pid: Pid, data: Vec<u8>) -> Result<(), SendError>

Sends a raw message to another process.

Source

pub fn send<M: Term>(&self, pid: Pid, msg: &M) -> Result<(), SendError>

Sends a typed message to another process.

Source

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.

Source

pub fn is_trapping_exits(&self) -> bool

Returns whether this process is trapping exits.

Creates a bidirectional link with another process.

If either process terminates abnormally, the other will receive an exit signal.

Removes a link with another process.

Source

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.

Source

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.

Source

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.

Source

pub fn whereis(&self, name: &str) -> Option<Pid>

Looks up a process by registered name.

Source

pub fn register(&self, name: String) -> bool

Registers a name for this process.

Returns false if the name is already taken.

Source

pub fn unregister(&self, name: &str) -> Option<Pid>

Unregisters a name.

Source

pub fn is_alive(&self, pid: Pid) -> bool

Returns true if the given process is alive.

Trait Implementations§

Source§

impl Debug for Context

Source§

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

Formats the value using the given formatter. 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> 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 = 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.