pub struct PkPromise { /* private fields */ }Available on crate feature
std only.Expand description
A simple implementation of Pollable that executes tasks in a background thread.
This is similar to a JavaScript Promise. It allows offloading work to another thread
and polling for its completion within the PK protocol’s poll() cycle.
Note: This is only available when the std feature is enabled.
Implementations§
Source§impl PkPromise
impl PkPromise
Sourcepub fn execute<T>(function: T) -> Pin<Box<Self>>
pub fn execute<T>(function: T) -> Pin<Box<Self>>
Executes a closure in a new thread and returns a Pollable handle.
The provided closure function receives a resolve callback. Call resolve(data)
when the task is complete to make the data available.
§Arguments
function: A closure that performs the task and calls the providedresolvecallback.
§Example
use pk_command::PkPromise;
let promise = PkPromise::execute(|resolve| {
// Do expensive work...
resolve(b"done".to_vec());
});§Example with PK Command
use pk_command::{PkCommand, PkCommandConfig, PkHashmapMethod, PkHashmapVariable, PkPromise};
let vars = PkHashmapVariable::new(vec![]);
let methods = PkHashmapMethod::new(vec![(
"LONGT".to_string(),
Box::new(|param| {
PkPromise::execute(|resolve| {
// do_something_with(param);
resolve(b"task complete".to_vec());
})
}),
)]);
let config = PkCommandConfig::default(64);
let pk = PkCommand::<_, _, std::time::Instant>::new(config, vars, methods);
// main loop...Trait Implementations§
Auto Trait Implementations§
impl Freeze for PkPromise
impl RefUnwindSafe for PkPromise
impl Send for PkPromise
impl Sync for PkPromise
impl Unpin for PkPromise
impl UnwindSafe for PkPromise
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