EnginePool

Struct EnginePool 

Source
pub struct EnginePool { /* private fields */ }
Expand description

线程局部引擎池

§使用场景

  • ✅ 单线程内需要多个引擎实例
  • ✅ 避免频繁创建引擎的开销
  • ✅ 需要环境隔离的高频调用
  • ⚠️ 每个线程有独立的池(不跨线程共享)

§特点

  • 线程局部:每个线程有独立的引擎池
  • 自动管理:RAII模式,使用完自动归还
  • 环境隔离:每次获取前清空变量
  • AST缓存:每个引擎独立维护缓存

§示例

use aether::engine::EnginePool;

// 创建线程局部引擎池
let pool = EnginePool::new(4);

// 使用引擎
{
    let mut engine = pool.acquire();
    let result = engine.eval("Set X 10\n(X + 20)").unwrap();
    println!("Result: {}", result);
} // engine 自动归还到池中

// 多次使用(复用引擎实例)
for i in 0..100 {
    let mut engine = pool.acquire();
    let code = format!("Set X {}\n(X * 2)", i);
    engine.eval(&code).unwrap();
}

§与 GlobalEngine 对比

  • GlobalEngine: 单个引擎实例,适合简单场景
  • EnginePool: 多个引擎实例,避免频繁创建开销

如果你只需要一个引擎实例,使用 GlobalEngine 更简单。

Implementations§

Source§

impl EnginePool

Source

pub fn new(capacity: usize) -> Self

创建新的引擎池

§参数
  • capacity: 池大小
§示例
use aether::engine::EnginePool;

// 创建容量为4的引擎池
let pool = EnginePool::new(4);
Source

pub fn acquire(&mut self) -> PooledEngine

从池中获取引擎(自动归还)

如果池中没有可用引擎,会创建临时引擎。 返回的 PooledEngine 会在作用域结束时自动归还到池中。

§环境隔离

每次获取前自动清空环境变量,保证隔离性。

Source

pub fn capacity(&self) -> usize

获取池的容量

Source

pub fn available(&self) -> usize

获取池中当前可用的引擎数量

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.