use core::{iter, pin::Pin};
use super::{Address, ConnectionType, LogLevel, MultiStreamAddress, PlatformRef};
use alloc::{borrow::Cow, format, string::String};
#[derive(Debug, Clone)]
pub struct WithPrefix<T> {
inner: T,
prefix: String,
}
impl<T> WithPrefix<T> {
pub const fn new(prefix: String, inner: T) -> Self {
WithPrefix { inner, prefix }
}
}
impl<T: PlatformRef> PlatformRef for WithPrefix<T> {
type Delay = T::Delay;
type Instant = T::Instant;
type MultiStream = T::MultiStream;
type Stream = T::Stream;
type ReadWriteAccess<'a> = T::ReadWriteAccess<'a>;
type StreamErrorRef<'a> = T::StreamErrorRef<'a>;
type StreamConnectFuture = T::StreamConnectFuture;
type MultiStreamConnectFuture = T::MultiStreamConnectFuture;
type StreamUpdateFuture<'a> = T::StreamUpdateFuture<'a>;
type NextSubstreamFuture<'a> = T::NextSubstreamFuture<'a>;
fn now_from_unix_epoch(&self) -> core::time::Duration {
self.inner.now_from_unix_epoch()
}
fn now(&self) -> Self::Instant {
self.inner.now()
}
fn fill_random_bytes(&self, buffer: &mut [u8]) {
self.inner.fill_random_bytes(buffer)
}
fn sleep(&self, duration: core::time::Duration) -> Self::Delay {
self.inner.sleep(duration)
}
fn sleep_until(&self, when: Self::Instant) -> Self::Delay {
self.inner.sleep_until(when)
}
fn spawn_task(
&self,
task_name: Cow<str>,
task: impl futures_util::future::Future<Output = ()> + Send + 'static,
) {
self.inner
.spawn_task(Cow::Owned(format!("{}-{}", self.prefix, task_name)), task)
}
fn log<'a>(
&self,
log_level: LogLevel,
log_target: &'a str,
message: &'a str,
mut key_values: impl Iterator<Item = (&'a str, &'a dyn core::fmt::Display)>,
) {
self.inner.log(
log_level,
&format!("{}-{}", self.prefix, log_target),
message,
iter::from_fn(move || key_values.next()),
)
}
fn client_name(&self) -> Cow<str> {
self.inner.client_name()
}
fn client_version(&self) -> Cow<str> {
self.inner.client_version()
}
fn supports_connection_type(&self, connection_type: ConnectionType) -> bool {
self.inner.supports_connection_type(connection_type)
}
fn connect_stream(&self, address: Address) -> Self::StreamConnectFuture {
self.inner.connect_stream(address)
}
fn connect_multistream(&self, address: MultiStreamAddress) -> Self::MultiStreamConnectFuture {
self.inner.connect_multistream(address)
}
fn open_out_substream(&self, connection: &mut Self::MultiStream) {
self.inner.open_out_substream(connection)
}
fn next_substream<'a>(
&self,
connection: &'a mut Self::MultiStream,
) -> Self::NextSubstreamFuture<'a> {
self.inner.next_substream(connection)
}
fn read_write_access<'a>(
&self,
stream: Pin<&'a mut Self::Stream>,
) -> Result<Self::ReadWriteAccess<'a>, Self::StreamErrorRef<'a>> {
self.inner.read_write_access(stream)
}
fn wait_read_write_again<'a>(
&self,
stream: Pin<&'a mut Self::Stream>,
) -> Self::StreamUpdateFuture<'a> {
self.inner.wait_read_write_again(stream)
}
}