pub struct WeakCompletionStage<T: Send + Sync + 'static>(/* private fields */);Expand description
Weak reference to a completion stage.
This form of completion stage is only useful in certain rare complex use cases.
§Example
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use completion_stage::{CompletionStage, WeakCompletionStage};
#[derive(Debug)]
struct Connection {
//...
}
static PENDING_CONNECTIONS: Mutex<Vec<WeakCompletionStage<Connection>>> = Mutex::new(Vec::new());
fn connection_factory_thread() {
let connection: CompletionStage<Connection> = CompletionStage::new_async::<std::thread::Thread>(|| {
std::thread::sleep(Duration::from_secs(10));
//...
Connection {
//...
}
});
PENDING_CONNECTIONS.lock().unwrap().push(connection.new_weak());
/// Do something else...
if !connection.completed() {
// We don't want to wait anymore...
// And we can't remove from PENDING_CONNECTIONS because
// getting the lock is too costly here for "whatever" reason because we need to urgently do something else.
// Anyway, this causes the strong reference count to go to 0 once the thread above finishes.
return;
}
//... use it
}
fn watcher_thread() {
loop {
thread::sleep(Duration::from_secs(60));
// Kill all orphans
PENDING_CONNECTIONS.lock().unwrap().retain(|c| c.strong_ref_count() > 0);
// ...
// 'Clone' all pending for "inspection"
let cloned = PENDING_CONNECTIONS.lock().unwrap().clone();
for connection in cloned.into_iter().filter_map(|c| c.upgrade()) {
if connection.completed() {
//Do something with it...
}
}
}
}Implementations§
Source§impl<T: Send + Sync + 'static> WeakCompletionStage<T>
impl<T: Send + Sync + 'static> WeakCompletionStage<T>
pub fn upgrade(&self) -> Option<CompletionStage<T>>
Sourcepub fn strong_ref_count(&self) -> usize
pub fn strong_ref_count(&self) -> usize
Returns the strong reference count of this stage.
§Note
This function just calls Weak::strong_count and is therefore subject to the same
restrictions/limitations.
Sourcepub fn weak_ref_count(&self) -> usize
pub fn weak_ref_count(&self) -> usize
Returns the weak reference count of this stage.
This counter may return 0 if the strong_ref_count is 0.
§Note
This function just calls Arc::weak_count and is therefore subject to the same
restrictions/limitations.
Sourcepub fn complete(&self, completion: Completion<T>) -> Option<Completion<T>>
pub fn complete(&self, completion: Completion<T>) -> Option<Completion<T>>
Complete the stage with the given completion.
§Returns
- None if the call completed the stage.
- Some if this stage has no strong references remaining or if the stage is already completed.
Sourcepub fn complete_with_value(&self, value: T) -> Option<T>
pub fn complete_with_value(&self, value: T) -> Option<T>
Complete the stage with the given value.
§Returns
- None if the call completed the stage.
- Some if this stage has no strong references remaining or if the stage is already completed.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for WeakCompletionStage<T>
impl<T> !RefUnwindSafe for WeakCompletionStage<T>
impl<T> Send for WeakCompletionStage<T>
impl<T> Sync for WeakCompletionStage<T>
impl<T> Unpin for WeakCompletionStage<T>
impl<T> UnsafeUnpin for WeakCompletionStage<T>
impl<T> !UnwindSafe for WeakCompletionStage<T>
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