January 2019
Intermediate to advanced
520 pages
14h 32m
English
To add support for incoming message types, we have to implement the Handler trait for our actor. Let's implement Handler of Count messages for our CountActor:
impl Handler<Count> for CountActor { type Result = Value; fn handle(&mut self, Count(path): Count, _: &mut Context<Self>) -> Self::Result { let value = self.counter.entry(path).or_default(); *value = *value + 1; *value }}
We also have to set the associated type with the same type of a result.
Handling occurs in the body of the handle method of the Handler trait. We'll get entry for a provided String value with the Count message and extract the entry of HashMap. If no entry is found, we'll get a default value that equals 0 for u64 type (Value alias) and add 1 to that value.
Read now
Unlock full access