pub struct ThreadSafeEngine { /* private fields */ }Expand description
Wrapper that makes non-Send engines usable across threads.
Internally an Arc<Mutex<Box<dyn ScriptEngine>>>; clones share the same
underlying engine.
Implementations§
Source§impl ThreadSafeEngine
impl ThreadSafeEngine
Sourcepub fn new(engine: Box<dyn ScriptEngine>) -> Self
pub fn new(engine: Box<dyn ScriptEngine>) -> Self
Wrap an existing engine.
Examples found in repository?
examples/thread_safe.rs (line 11)
10fn main() -> postscriptum::Result<()> {
11 let engine = ThreadSafeEngine::new(get_engine(EngineType::Rhai));
12 engine.eval("let x = 0")?;
13
14 let mut handles = Vec::new();
15 for i in 0..4 {
16 let engine = engine.clone();
17 handles.push(thread::spawn(move || {
18 engine.eval(&format!("x = x + {i}; x")).unwrap()
19 }));
20 }
21
22 for handle in handles {
23 println!("thread result: {}", handle.join().unwrap());
24 }
25 Ok(())
26}Sourcepub fn new_with_bundle(engine_type: EngineType, bundle: &FunctionBundle) -> Self
pub fn new_with_bundle(engine_type: EngineType, bundle: &FunctionBundle) -> Self
Create a thread-safe engine of the given type, registering bundle’s
functions. This wraps get_engine_with_bundle.
Sourcepub fn eval(&self, code: &str) -> Result<String>
pub fn eval(&self, code: &str) -> Result<String>
Evaluate code on the wrapped engine, returning the result string.
Examples found in repository?
examples/thread_safe.rs (line 12)
10fn main() -> postscriptum::Result<()> {
11 let engine = ThreadSafeEngine::new(get_engine(EngineType::Rhai));
12 engine.eval("let x = 0")?;
13
14 let mut handles = Vec::new();
15 for i in 0..4 {
16 let engine = engine.clone();
17 handles.push(thread::spawn(move || {
18 engine.eval(&format!("x = x + {i}; x")).unwrap()
19 }));
20 }
21
22 for handle in handles {
23 println!("thread result: {}", handle.join().unwrap());
24 }
25 Ok(())
26}Trait Implementations§
Source§impl Clone for ThreadSafeEngine
impl Clone for ThreadSafeEngine
impl Send for ThreadSafeEngine
impl Sync for ThreadSafeEngine
Auto Trait Implementations§
impl Forget for ThreadSafeEngine
impl Freeze for ThreadSafeEngine
impl Move for ThreadSafeEngine
impl RefUnwindSafe for ThreadSafeEngine
impl Unpin for ThreadSafeEngine
impl UnsafeUnpin for ThreadSafeEngine
impl UnwindSafe for ThreadSafeEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> Ungil for Twhere
T: Send,
Source§impl<T> Variant for T
impl<T> Variant for T
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert this
Variant trait object to &mut dyn Any.Source§fn as_boxed_any(self: Box<T>) -> Box<dyn Any>
fn as_boxed_any(self: Box<T>) -> Box<dyn Any>
Convert this
Variant trait object to Box<dyn Any>.