crb_runtime/context.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
//! A context for composable blocks.
use crate::interruptor::Controller;
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
/// A commont methods of all contexts and spans for tracing and logging.
///
/// The have provide a reference to a label.
pub trait Context: Send {
/// An address to interact with the context.
type Address: Send + Clone;
// A label that used for logging all events around the context.
// fn label(&self) -> &Label;
/// A reference to an address.
fn address(&self) -> &Self::Address;
}
/// The main features of composable block's context.
///
/// It could be interrupted and contains a method to check a life status of a composable block.
pub trait ManagedContext: Context {
fn controller(&self) -> &Controller;
/// Marks a context as interrupted.
fn shutdown(&mut self);
}
/*
/// `Label` is a `Context` for cases when
/// context is not necessary, but for many
/// runtimes at least `Label` is required
/// for tracing.
impl Context for Label {
type Address = ();
fn label(&self) -> &Label {
&self
}
fn address(&self) -> &Self::Address {
&()
}
}
*/
/// A unique label of an activity.
///
/// Every task with a context has a unique label.
// TODO: Use tracing/telemetry spans here
#[derive(Debug, Clone)]
pub struct Label {
// TODO: Add `Span` here
name: Arc<String>,
}
impl Label {
/// Creates a new label.
pub fn new(name: String) -> Self {
Self {
name: Arc::new(name),
}
}
/// Creates a new label by stacking the existent and a new value.
pub fn stack(&self, name: &str) -> Self {
let name = format!("{}::{}", self.name, name);
Self {
name: Arc::new(name),
}
}
}
impl PartialEq for Label {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.name, &other.name)
}
}
impl AsRef<str> for Label {
fn as_ref(&self) -> &str {
self.name.as_ref()
}
}
impl Deref for Label {
type Target = str;
fn deref(&self) -> &Self::Target {
&*self.name
}
}
impl fmt::Display for Label {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.name.fmt(f)
}
}