use std::{ error::Error as StdError, fmt::{Debug, Display}, future::Future, hash::{Hash, Hasher}, ops::Deref, pin::Pin, sync::Arc, task::{Context, Poll}, time::Duration, }; use anyhow::{Error, anyhow}; use pin_project_lite::pin_project; use serde::{Deserialize, Deserializer, Serialize, Serializer}; pub use super::{ id_factory::{IdFactory, IdFactoryWithReuse}, once_map::*, }; /// A error struct that is backed by an Arc to allow cloning errors #[derive(Debug, Clone)] pub struct SharedError { inner: Arc, } impl SharedError { pub fn new(err: anyhow::Error) -> Self { Self { inner: Arc::new(err), } } } impl AsRef for SharedError { fn as_ref(&self) -> &(dyn StdError + 'static) { let err: &anyhow::Error = &self.inner; err.as_ref() } } impl StdError for SharedError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { self.inner.source() } fn provide<'a>(&'a self, req: &mut std::error::Request<'a>) { self.inner.provide(req); } } impl Display for SharedError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Display::fmt(&*self.inner, f) } } impl From for SharedError { fn from(e: Error) -> Self { Self::new(e) } } impl PartialEq for SharedError { fn eq(&self, other: &Self) -> bool { Arc::ptr_eq(&self.inner, &other.inner) } } impl Eq for SharedError {} impl Serialize for SharedError { fn serialize(&self, serializer: S) -> Result { let mut v = vec![self.to_string()]; let mut source = self.source(); while let Some(s) = source { v.push(s.to_string()); source = s.source(); } Serialize::serialize(&v, serializer) } } impl<'de> Deserialize<'de> for SharedError { fn deserialize>(deserializer: D) -> Result { use serde::de::Error; let mut messages = >::deserialize(deserializer)?; let mut e = match messages.pop() { Some(e) => anyhow!(e), None => return Err(Error::custom("expected at least 1 error message")), }; while let Some(message) = messages.pop() { e = e.context(message); } Ok(SharedError::new(e)) } } impl Deref for SharedError { type Target = Arc; fn deref(&self) -> &Self::Target { &self.inner } } pub struct FormatDuration(pub Duration); impl Display for FormatDuration { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let s = self.0.as_secs(); if s > 10 { return write!(f, "{s}s"); } let ms = self.0.as_millis(); if ms > 10 { return write!(f, "{ms}ms"); } write!(f, "{}ms", (self.0.as_micros() as f32) / 1000.0) } } impl Debug for FormatDuration { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let s = self.0.as_secs(); if s > 100 { return write!(f, "{s}s"); } let ms = self.0.as_millis(); if ms > 10000 { return write!(f, "{:.2}s", (ms as f32) / 1000.0); } if ms > 100 { return write!(f, "{ms}ms"); } write!(f, "{}ms", (self.0.as_micros() as f32) / 1000.0) } } pub struct FormatBytes(pub usize); impl Display for FormatBytes { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let b = self.0; const KB: usize = 1_024; const MB: usize = 1_024 * KB; const GB: usize = 1_024 * MB; if b > GB { return write!(f, "{:.2}GiB", ((b / MB) as f32) / 1_024.0); } if b > MB { return write!(f, "{:.2}MiB", ((b / KB) as f32) / 1_024.0); } if b > KB { return write!(f, "{:.2}KiB", (b as f32) / 1_024.0); } write!(f, "{b}B") } } /// Smart pointer that stores data either in an [Arc] or as a static reference. pub enum StaticOrArc { Static(&'static T), Shared(Arc), } impl AsRef for StaticOrArc { fn as_ref(&self) -> &T { match self { Self::Static(s) => s, Self::Shared(b) => b, } } } impl From<&'static T> for StaticOrArc { fn from(s: &'static T) -> Self { Self::Static(s) } } impl From> for StaticOrArc { fn from(b: Arc) -> Self { Self::Shared(b) } } impl From for StaticOrArc { fn from(b: T) -> Self { Self::Shared(Arc::new(b)) } } impl Deref for StaticOrArc { type Target = T; fn deref(&self) -> &Self::Target { self.as_ref() } } impl Clone for StaticOrArc { fn clone(&self) -> Self { match self { Self::Static(s) => Self::Static(s), Self::Shared(b) => Self::Shared(b.clone()), } } } impl PartialEq for StaticOrArc { fn eq(&self, other: &Self) -> bool { **self == **other } } impl Eq for StaticOrArc {} impl Hash for StaticOrArc { fn hash(&self, state: &mut H) { (**self).hash(state); } } impl Display for StaticOrArc { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { (**self).fmt(f) } } impl Debug for StaticOrArc { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { (**self).fmt(f) } } pin_project! { /// A future that wraps another future and applies a function on every poll call. pub struct WrapFuture { wrapper: W, #[pin] future: F, } } impl Fn(Pin<&mut F>, &mut Context<'a>) -> Poll> WrapFuture { pub fn new(future: F, wrapper: W) -> Self { Self { wrapper, future } } } impl Fn(Pin<&mut F>, &mut Context<'a>) -> Poll> Future for WrapFuture { type Output = F::Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); (this.wrapper)(this.future, cx) } }