pub struct WorkerContext {
pub info: WorkerInfo,
pub cancellation: CancellationToken,
pub deadline: Deadline,
}Expand description
Cancellation and deadline state passed to worker lifecycle callbacks.
For persistent pools this context belongs to the pool lifetime: its token is cancelled only when the owner shuts the pool down. Dataset fetches and task transforms receive a separate, fresh context for each iterator generation.
use std::time::Duration;
use rusttorch_data::{
CancellationToken, Deadline, LoaderCancelled, WaitOutcome,
WorkerContext, WorkerInfo,
};
let cancellation = CancellationToken::new();
let context = WorkerContext::new(
WorkerInfo::new(0, 1, 7, 0)?,
cancellation.clone(),
Deadline::none(),
);
assert!(context.check().is_ok());
let cancel = std::thread::spawn(move || cancellation.cancel());
assert_eq!(context.wait_cancelled_or_deadline(), WaitOutcome::Cancelled);
cancel.join().unwrap();
let _: LoaderCancelled = context.check().unwrap_err();
let expired = WorkerContext::new(
WorkerInfo::new(0, 1, 7, 0)?,
CancellationToken::new(),
Deadline::after(Duration::ZERO),
);
assert_eq!(
expired.wait_cancelled_or_deadline(),
WaitOutcome::DeadlineExpired,
);Fields§
§info: WorkerInfoStable worker identity and initialization seed.
cancellation: CancellationTokenCooperative cancellation token.
deadline: DeadlineDynamic monotonic deadline.
Implementations§
Source§impl WorkerContext
impl WorkerContext
Sourcepub fn new(
info: WorkerInfo,
cancellation: CancellationToken,
deadline: Deadline,
) -> Self
pub fn new( info: WorkerInfo, cancellation: CancellationToken, deadline: Deadline, ) -> Self
Creates a context from explicit worker information and controls.
Sourcepub fn check(&self) -> Result<(), LoaderCancelled>
pub fn check(&self) -> Result<(), LoaderCancelled>
Returns an error when cancelled or expired.
Sourcepub fn wait_cancelled_or_deadline(&self) -> WaitOutcome
pub fn wait_cancelled_or_deadline(&self) -> WaitOutcome
Blocks until cancellation or deadline expiry.
Trait Implementations§
Source§impl Clone for WorkerContext
impl Clone for WorkerContext
Source§fn clone(&self) -> WorkerContext
fn clone(&self) -> WorkerContext
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for WorkerContext
impl RefUnwindSafe for WorkerContext
impl Send for WorkerContext
impl Sync for WorkerContext
impl Unpin for WorkerContext
impl UnsafeUnpin for WorkerContext
impl UnwindSafe for WorkerContext
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