use std::ops::Deref;
use std::sync::{Arc, LazyLock};
use crate::{Encode, LazyChannel, LazyRawChannel};
use super::Context;
static DEFAULT_CONTEXT: LazyContext = LazyContext::new();
pub struct LazyContext(LazyLock<Arc<Context>>);
impl LazyContext {
#[allow(clippy::new_without_default)] pub const fn new() -> Self {
Self(LazyLock::new(Context::new))
}
pub const fn get_default() -> &'static Self {
&DEFAULT_CONTEXT
}
pub const fn channel<T: Encode>(&'static self, topic: &'static str) -> LazyChannel<T> {
LazyChannel::new(topic).context(self)
}
pub const fn raw_channel(
&'static self,
topic: &'static str,
message_encoding: &'static str,
) -> LazyRawChannel {
LazyRawChannel::new(topic, message_encoding).context(self)
}
}
impl Deref for LazyContext {
type Target = Arc<Context>;
fn deref(&self) -> &Self::Target {
&self.0
}
}