Skip to main content

bonsai_mdsl/
async_context.rs

1//! Async Context for Behavior Trees
2//!
3//! NOTE: This module is currently unused. Async behavior trees are fully supported
4//! using the regular TreeContext with external async task management, as demonstrated
5//! in the async_drone example which uses tokio::spawn and channels.
6//!
7//! This AsyncTreeContext was originally planned to provide native async actions
8//! that return Future<NodeResult>, but the external approach proves more flexible
9//! and follows standard Rust async patterns.
10
11#[cfg(feature = "async")]
12use serde_json::Value;
13#[cfg(feature = "async")]
14use std::collections::HashMap;
15#[cfg(feature = "async")]
16use std::future::Future;
17#[cfg(feature = "async")]
18use std::pin::Pin;
19
20#[cfg(feature = "async")]
21use crate::nodes::NodeResult;
22
23/// Function type for async actions (currently unused)
24#[cfg(feature = "async")]
25pub type AsyncActionFn = Box<
26    dyn Fn(&AsyncTreeContext, &[Value]) -> Pin<Box<dyn Future<Output = NodeResult> + Send>>
27        + Send
28        + Sync,
29>;
30
31/// Async context for executing behavior trees (currently unused)
32///
33/// NOTE: Async behavior trees work perfectly with the regular TreeContext
34/// using external async task management. See the async_drone example.
35#[cfg(feature = "async")]
36pub struct AsyncTreeContext {
37    _actions: HashMap<String, AsyncActionFn>,
38    _data: HashMap<String, Value>,
39}
40
41#[cfg(feature = "async")]
42impl AsyncTreeContext {
43    pub fn new() -> Self {
44        Self {
45            _actions: HashMap::new(),
46            _data: HashMap::new(),
47        }
48    }
49
50    // This was planned to provide native async behavior tree execution
51    // However, external async task management (as shown in async_drone example)
52    // is more flexible and follows standard Rust patterns:
53    // - Use tokio::spawn for async tasks
54    // - Use channels for communication between tasks and behavior tree
55    // - Use regular TreeContext with custom actions that manage async state
56}
57
58#[cfg(feature = "async")]
59impl Default for AsyncTreeContext {
60    fn default() -> Self {
61        Self::new()
62    }
63}