mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-02 17:15:43 +00:00
utils: use itertools
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
|
use crate::utils::MapTryInto;
|
||||||
|
use derivative::Derivative;
|
||||||
use derive_more::{Deref, DerefMut, From, IntoIterator};
|
use derive_more::{Deref, DerefMut, From, IntoIterator};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use crate::utils::MapTryInto;
|
|
||||||
|
|
||||||
pub trait MessageModel<Message: prost::Message>: Into<Message> + for<'m> TryFrom<&'m Message> {}
|
pub trait MessageModel<Message: prost::Message>:
|
||||||
|
Into<Message> + for<'m> TryFrom<&'m Message>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
impl<Message, Model> MessageModel<Message> for Model
|
impl<Message, Model> MessageModel<Message> for Model
|
||||||
where
|
where
|
||||||
@@ -13,16 +17,27 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(
|
||||||
Debug, Clone, PartialEq, Eq, Hash, From, Deref, DerefMut, Serialize, Deserialize, IntoIterator,
|
Derivative,
|
||||||
|
Debug,
|
||||||
|
Clone,
|
||||||
|
PartialEq,
|
||||||
|
Eq,
|
||||||
|
Hash,
|
||||||
|
From,
|
||||||
|
Deref,
|
||||||
|
DerefMut,
|
||||||
|
Serialize,
|
||||||
|
Deserialize,
|
||||||
|
IntoIterator,
|
||||||
)]
|
)]
|
||||||
|
#[derivative(Default(bound = ""))]
|
||||||
#[serde(transparent)]
|
#[serde(transparent)]
|
||||||
#[into_iterator(owned, ref, ref_mut)]
|
#[into_iterator(owned, ref, ref_mut)]
|
||||||
pub struct RepeatedMessageModel<Model>(Vec<Model>);
|
pub struct RepeatedMessageModel<Model>(Vec<Model>);
|
||||||
|
|
||||||
impl<Model> Default for RepeatedMessageModel<Model>
|
impl<Model> RepeatedMessageModel<Model> {
|
||||||
{
|
pub fn into_inner(self) -> Vec<Model> {
|
||||||
fn default() -> Self {
|
self.0
|
||||||
Self(vec![])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +49,7 @@ where
|
|||||||
type Error = <Model as TryFrom<&'m Message>>::Error;
|
type Error = <Model as TryFrom<&'m Message>>::Error;
|
||||||
|
|
||||||
fn try_from(value: &'m Vec<Message>) -> Result<Self, Self::Error> {
|
fn try_from(value: &'m Vec<Message>) -> Result<Self, Self::Error> {
|
||||||
Ok(Self(value.map_try_into().collect::<Result<Vec<_>, _>>()?))
|
Ok(Self(value.into_iter().map_try_into().try_collect()?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user