Skip to main content

AsyncChildContext

Trait AsyncChildContext 

Source
pub trait AsyncChildContext:
    Send
    + Sync
    + Debug {
    // Required methods
    fn parent_id(&self) -> &str;
    fn emit_output(&self, message: &str);
    fn emit_output_with_level(&self, message: &str, level: &str);
    fn spawn_child<'life0, 'async_trait>(
        &'life0 self,
        config: ChildConfig,
    ) -> Pin<Box<dyn Future<Output = Result<Box<dyn AsyncChildHandle>, SpawnError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn child_count(&self) -> usize;
    fn max_children(&self) -> usize;
    fn send_to_child(
        &self,
        child_id: &str,
        input: Value,
    ) -> Result<ChildResult, RunError>;
    fn clone_box(&self) -> Box<dyn AsyncChildContext>;

    // Provided method
    fn send_to_child_async(
        &self,
        _child_id: &str,
        _input: Value,
    ) -> Result<(), RunError> { ... }
}
Expand description

Async context provided to Children for runtime interaction.

This trait provides async versions of ChildContext methods for children that need to spawn async children.

§Example

#[async_trait]
impl AsyncRunnableChild for MyWorker {
    async fn run(&mut self, input: Value) -> ChildResult {
        if let Some(ctx) = &self.async_ctx {
            ctx.emit_output("Starting async work...");

            // Spawn an async child
            let config = ChildConfig::from_inline("sub-1", "...");
            if let Ok(mut handle) = ctx.spawn_child(config).await {
                let result = handle.run(input.clone()).await;
                // ...
            }
        }
        ChildResult::Ok(input)
    }
}

Required Methods§

Source

fn parent_id(&self) -> &str

Returns the parent’s ID (Component or Child that owns this context).

Source

fn emit_output(&self, message: &str)

Emits output to the parent (displayed to user via IO).

Source

fn emit_output_with_level(&self, message: &str, level: &str)

Emits output with a specific level.

Source

fn spawn_child<'life0, 'async_trait>( &'life0 self, config: ChildConfig, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn AsyncChildHandle>, SpawnError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Spawns a child and returns an async handle to control it.

§Arguments
  • config - Configuration for the child
§Returns

An async handle to the spawned child, or an error if spawn failed.

§Errors
Source

fn child_count(&self) -> usize

Returns the number of active children.

Source

fn max_children(&self) -> usize

Returns the maximum allowed children.

Source

fn send_to_child( &self, child_id: &str, input: Value, ) -> Result<ChildResult, RunError>

Sends input to a child by ID and returns its result.

§Arguments
  • child_id - The child’s ID
  • input - Input data to pass to the child
Source

fn clone_box(&self) -> Box<dyn AsyncChildContext>

Clones this context into a boxed trait object.

Provided Methods§

Source

fn send_to_child_async( &self, _child_id: &str, _input: Value, ) -> Result<(), RunError>

Sends input to a child asynchronously (fire-and-forget).

Returns immediately. The child runs in a background thread.

§Default Implementation

Returns an error indicating async send is not supported.

Trait Implementations§

Source§

impl Clone for Box<dyn AsyncChildContext>

Source§

fn clone(&self) -> Self

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

Implementors§