|
|
|
@ -10,23 +10,24 @@ use uuid::Uuid; |
|
|
|
|
use crate::{UID}; |
|
|
|
|
|
|
|
|
|
pub trait ToMsg: Clone { //+ Serialize + DeserializeOwned {
|
|
|
|
|
fn as_message<'m>(&'m self) -> Message<'m, Self> |
|
|
|
|
fn as_message<'m>(&'m self) -> BaseMessage<'m, Self> |
|
|
|
|
where Cow<'m, Self>: From<&'m Self> { |
|
|
|
|
Message::new(self) |
|
|
|
|
BaseMessage::new(self) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 1. Cow<'_, ItemWrap<I>> - failed, Message::new needs val or ref
|
|
|
|
|
// 2. ItemWrap<Cow<'_, I>> - can't impl From<Vec<...>> for Cow
|
|
|
|
|
fn into_message(self) -> BaseMessage<'static, Self> { |
|
|
|
|
BaseMessage::new_cow(self) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)] |
|
|
|
|
pub struct Message<'cow, I> |
|
|
|
|
pub struct BaseMessage<'cow, I> |
|
|
|
|
where I: ToMsg { |
|
|
|
|
pub id: Uuid, |
|
|
|
|
pub item: Cow<'cow, I> |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'cow, I> Message<'cow, I> |
|
|
|
|
impl<'cow, I> BaseMessage<'cow, I> |
|
|
|
|
where I: ToMsg |
|
|
|
|
{ |
|
|
|
|
pub fn new<C>(item: C) -> Self |
|
|
|
@ -37,6 +38,13 @@ impl<'cow, I> Message<'cow, I> |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn new_cow(item: I) -> Self { |
|
|
|
|
Self { |
|
|
|
|
id: UID.clone(), |
|
|
|
|
item: Cow::Owned(item) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn into_item(self) -> I { |
|
|
|
|
self.item.into_owned() |
|
|
|
|
} |
|
|
|
@ -45,26 +53,7 @@ impl<'cow, I> Message<'cow, I> |
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)] |
|
|
|
|
pub struct RawMsg(pub String); |
|
|
|
|
|
|
|
|
|
impl<T: ToMsg> ToMsg for Vec<T> {} //TODO: impl this for all collections
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)] |
|
|
|
|
pub struct ItemWrap<T: ToMsg>(pub T); |
|
|
|
|
|
|
|
|
|
impl<T: ToMsg> ToMsg for ItemWrap<T> {} |
|
|
|
|
|
|
|
|
|
impl<'cow, T: ToMsg> From<ItemWrap<T>> for Cow<'cow, ItemWrap<T>> { |
|
|
|
|
fn from(obj: ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> { |
|
|
|
|
Cow::Owned(obj) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'cow, T: ToMsg> From<&'cow ItemWrap<T>> for Cow<'cow, ItemWrap<T>> { |
|
|
|
|
fn from(obj: &'cow ItemWrap<T>) -> Cow<'cow, ItemWrap<T>> { |
|
|
|
|
Cow::Borrowed(obj) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<T: ToMsg> ToMsg for Vec<T> {} |
|
|
|
|
/* |
|
|
|
|
#[cfg(test)] |
|
|
|
|
mod tests { |
|
|
|
@ -73,11 +62,11 @@ mod tests { |
|
|
|
|
#[test] |
|
|
|
|
fn test_create_message_owned() { |
|
|
|
|
let item = String::from("QWEDSA"); |
|
|
|
|
let msg_raw = Message { |
|
|
|
|
let msg_raw = BaseMessage { |
|
|
|
|
id: *UID, |
|
|
|
|
item: Cow::Owned(item.clone()) |
|
|
|
|
}; |
|
|
|
|
let msg = Message::new(item); |
|
|
|
|
let msg = BaseMessage::new(item); |
|
|
|
|
assert_eq!(msg_raw.item, msg.item); |
|
|
|
|
} |
|
|
|
|
}*/ |