|
|
@ -1,16 +1,15 @@ |
|
|
|
use uuid::Uuid; |
|
|
|
use uuid::Uuid; |
|
|
|
use serde::{ |
|
|
|
use serde::{ |
|
|
|
Serialize, |
|
|
|
Serialize, |
|
|
|
Deserialize |
|
|
|
Deserialize, |
|
|
|
|
|
|
|
de::DeserializeOwned, |
|
|
|
}; |
|
|
|
}; |
|
|
|
use std::{ |
|
|
|
use std::{ |
|
|
|
borrow::Cow |
|
|
|
borrow::Cow |
|
|
|
}; |
|
|
|
}; |
|
|
|
use crate::{UID, Uid}; |
|
|
|
use crate::{UID, Uid}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub trait ToMsg: Clone + Serialize + DeserializeOwned { |
|
|
|
pub trait ToMsg |
|
|
|
|
|
|
|
where Self: Clone { |
|
|
|
|
|
|
|
fn as_message<'m>(&'m self) -> Message<'m, Self> |
|
|
|
fn as_message<'m>(&'m self) -> Message<'m, Self> |
|
|
|
where Cow<'m, Self>: From<&'m Self> { |
|
|
|
where Cow<'m, Self>: From<&'m Self> { |
|
|
|
Message::new(self) |
|
|
|
Message::new(self) |
|
|
@ -19,13 +18,13 @@ where Self: Clone { |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)] |
|
|
|
#[derive(Serialize, Deserialize, Debug)] |
|
|
|
pub struct Message<'cow, I> |
|
|
|
pub struct Message<'cow, I> |
|
|
|
where I: Clone { |
|
|
|
where I: ToMsg { |
|
|
|
pub id: Uid, |
|
|
|
pub id: Uid, |
|
|
|
pub item: Cow<'cow, I> |
|
|
|
pub item: Cow<'cow, I> |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<'cow, I> Message<'cow, I> |
|
|
|
impl<'cow, I> Message<'cow, I> |
|
|
|
where I: Clone |
|
|
|
where I: ToMsg |
|
|
|
{ |
|
|
|
{ |
|
|
|
pub fn new<C>(item: C) -> Self |
|
|
|
pub fn new<C>(item: C) -> Self |
|
|
|
where C: Into<Cow<'cow, I>> { |
|
|
|
where C: Into<Cow<'cow, I>> { |
|
|
@ -44,31 +43,26 @@ impl<'cow, I> Message<'cow, I> |
|
|
|
pub struct RawMsg(pub String); |
|
|
|
pub struct RawMsg(pub String); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// because can't impl From<ItemWrap<...>> for Cow
|
|
|
|
// because can't impl From<Vec<...>> for Cow
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)] |
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)] |
|
|
|
struct ItemWrap<T>(T); |
|
|
|
pub struct ItemWrap<T: ToMsg>(pub T); |
|
|
|
|
|
|
|
|
|
|
|
impl<T> ItemWrap<T> { |
|
|
|
impl<T: ToMsg> ItemWrap<T> { |
|
|
|
pub fn into_inner(self) -> T { |
|
|
|
pub fn into_inner(self) -> T { |
|
|
|
self.0 |
|
|
|
self.0 |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<T> From<T> for ItemWrap<T> { |
|
|
|
impl<T: ToMsg> ToMsg for ItemWrap<T> {} |
|
|
|
fn from(t: T) -> Self { |
|
|
|
//impl<T: ToMsg> ToMsg for Vec<T> {}
|
|
|
|
ItemWrap(t) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: Clone> ToMsg for ItemWrap<T> {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'cow, T: Clone> From<ItemWrap<T>> for Cow<'cow, ItemWrap<T>> { |
|
|
|
impl<'cow, T: ToMsg> From<ItemWrap<T>> for Cow<'cow, ItemWrap<T>> { |
|
|
|
fn from(obj: ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> { |
|
|
|
fn from(obj: ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> { |
|
|
|
Cow::Owned(obj) |
|
|
|
Cow::Owned(obj) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<'cow, T: Clone> From<&'cow ItemWrap<T>> for Cow<'cow, ItemWrap<T>> { |
|
|
|
impl<'cow, T: ToMsg> From<&'cow ItemWrap<T>> for Cow<'cow, ItemWrap<T>> { |
|
|
|
fn from(obj: &'cow ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> { |
|
|
|
fn from(obj: &'cow ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> { |
|
|
|
Cow::Borrowed(obj) |
|
|
|
Cow::Borrowed(obj) |
|
|
|
} |
|
|
|
} |
|
|
|