use crate::utils::VecDisplay; use crate::UID; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::fmt::Display; use uuid::Uuid; pub struct Moo<'cow, T: AsMsg + Clone>(pub Cow<'cow, T>); pub trait AsMsg: Clone + Serialize { fn as_message(&self) -> BaseMessage<'_, Self> { BaseMessage::new(self) } } impl<'cow, M: AsMsg> From for Moo<'cow, M> { #[inline] fn from(obj: M) -> Moo<'cow, M> { Moo(Cow::Owned(obj)) } } impl<'cow, M: AsMsg> From<&'cow M> for Moo<'cow, M> { #[inline] fn from(obj: &'cow M) -> Moo<'cow, M> { Moo(Cow::Borrowed(obj)) } } impl AsMsg for Vec {} impl AsMsg for VecDisplay {} impl<'msg, M: AsMsg> AsMsg for &'msg [M] {} #[derive(Serialize, Deserialize, Debug)] pub struct BaseMessage<'cow, I: AsMsg> { pub id: Uuid, inner: Cow<'cow, I>, } impl<'cow, I: AsMsg> BaseMessage<'cow, I> { pub fn new(inner: C) -> Self where C: Into>, { let Moo(inner) = inner.into(); Self { id: *UID, inner } } pub fn into_inner(self) -> I { self.inner.into_owned() } pub fn inner_ref(&self) -> &I { self.inner.as_ref() } }