mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-25 19:45:05 +08:00
simplify the trait a bit more by getting rids of the downcast_aggregate method
This commit is contained in:
parent
73b5722896
commit
ac919df37d
@ -93,26 +93,6 @@ pub trait Aggregate: 'static + mopa::Any + Send {
|
|||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
||||||
/// An internal helper function, you shouldn't implement it yourself.
|
|
||||||
/// This function should always be called on the same type. If `this` and `other`
|
|
||||||
/// aren't the same type behind the function will do nothing and return `None`.
|
|
||||||
fn downcast_aggregate(
|
|
||||||
old: Box<dyn Aggregate>,
|
|
||||||
new: Box<dyn Aggregate>,
|
|
||||||
) -> Option<Box<dyn Aggregate>>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
if old.is::<Self>() && new.is::<Self>() {
|
|
||||||
// Both the two following lines cannot fail, but just to be sure we don't crash, we're still avoiding unwrapping
|
|
||||||
let this = old.downcast::<Self>().ok()?;
|
|
||||||
let other = new.downcast::<Self>().ok()?;
|
|
||||||
Some(Self::aggregate(this, other))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Converts your structure to the final event that'll be sent to segment.
|
/// Converts your structure to the final event that'll be sent to segment.
|
||||||
fn into_event(self: Box<Self>) -> serde_json::Value;
|
fn into_event(self: Box<Self>) -> serde_json::Value;
|
||||||
}
|
}
|
||||||
|
@ -82,6 +82,22 @@ pub struct Event {
|
|||||||
total: usize,
|
total: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This function should always be called on the same type. If `this` and `other`
|
||||||
|
/// aren't the same type the function will do nothing and return `None`.
|
||||||
|
fn downcast_aggregate<ConcreteType: Aggregate>(
|
||||||
|
old: Box<dyn Aggregate>,
|
||||||
|
new: Box<dyn Aggregate>,
|
||||||
|
) -> Option<Box<dyn Aggregate>> {
|
||||||
|
if old.is::<ConcreteType>() && new.is::<ConcreteType>() {
|
||||||
|
// Both the two following lines cannot fail, but just to be sure we don't crash, we're still avoiding unwrapping
|
||||||
|
let this = old.downcast::<ConcreteType>().ok()?;
|
||||||
|
let other = new.downcast::<ConcreteType>().ok()?;
|
||||||
|
Some(ConcreteType::aggregate(this, other))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
pub fn new<T: Aggregate>(event: T, request: &HttpRequest) -> Self {
|
pub fn new<T: Aggregate>(event: T, request: &HttpRequest) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -92,7 +108,7 @@ impl Message {
|
|||||||
user_agents: extract_user_agents(request),
|
user_agents: extract_user_agents(request),
|
||||||
total: 1,
|
total: 1,
|
||||||
},
|
},
|
||||||
aggregator_function: T::downcast_aggregate,
|
aggregator_function: downcast_aggregate::<T>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user