Repository: servo/servo
File: components/devtools/actor.rs (Rust)

Code before:
    /// Create a name prefix for each actor type.
    /// While not needed for unique ids as each actor already has a different
    /// suffix, it can be used to visually identify actors in the logs.
    pub fn base_name<T: Actor>() -> &'static str {
        let prefix = type_name::<T>();
        prefix.split("::").last().unwrap_or(prefix)
    }

    /// Create a unique name based on a monotonically increasing suffix
    /// TODO: Merge this with `register/register_later` and don't allow to
    /// create new names without registering an actor.
    pub fn new_name<T: Actor>(&self) -> String {
        let suffix = self.next.fetch_add(1, Ordering::Relaxed);
        format!("{}{}", Self::base_name::<T>(), suffix)
    }

    /// Add an actor to the registry of known actors that can receive messages.
    pub(crate) fn register<T: Actor>(&self, actor: T) {
        self.actors
            .borrow_mut()
            .insert(actor.name(), Arc::new(actor));
    }

    /// Find an actor by registered name
    pub fn find<T: Actor>(&self, name: &str) -> DowncastableActorArc<T> {
        let actor = self.actors.borrow().get(name).unwrap().clone();
        DowncastableActorArc {
            actor,
            _phantom: PhantomData,
        }
    }

    /// Find an actor by registered name and return its serialization
    pub fn encode<T: ActorEncode<S>, S: Serialize>(&self, name: &str) -> S {
        self.find::<T>(name).encode(self)
    }

    /// Attempt to process a message as directed by its `to` property. If the actor is not found, does not support the
    /// message, or failed to handle the message, send an error reply instead.
    pub(crate) fn handle_message(
        &self,
        msg: &Map<String, Value>,
        stream: &mut TcpStream,
        stream_id: StreamId,
    ) -> Result<(), ()> {
        let to = match msg.get("to") {
            Some(to) => to.as_str().unwrap(),
            None => {
                log::warn!("Received unexpected message: {:?}", msg);
                return Err(());
            },

Review comment (suggestion):
```suggestion
        let actor = self.actors.borrow().get(name).expect("Should never look for a nonexistent actor").clone();
```

Code after:
    /// Create a name prefix for each actor type.
    /// While not needed for unique ids as each actor already has a different
    /// suffix, it can be used to visually identify actors in the logs.
    pub fn base_name<T: Actor>() -> &'static str {
        let prefix = type_name::<T>();
        prefix.split("::").last().unwrap_or(prefix)
    }

    /// Create a unique name based on a monotonically increasing suffix
    /// TODO: Merge this with `register/register_later` and don't allow to
    /// create new names without registering an actor.
    pub fn new_name<T: Actor>(&self) -> String {
        let suffix = self.next.fetch_add(1, Ordering::Relaxed);
        format!("{}{}", Self::base_name::<T>(), suffix)
    }

    /// Add an actor to the registry of known actors that can receive messages.
    pub(crate) fn register<T: Actor>(&self, actor: T) {
        self.actors
            .borrow_mut()
            .insert(actor.name(), Arc::new(actor));
    }

    /// Find an actor by registered name
    pub fn find<T: Actor>(&self, name: &str) -> DowncastableActorArc<T> {
        let actor = self
            .actors
            .borrow()
            .get(name)
            .expect("Should never look for a nonexistent actor")
            .clone();
        DowncastableActorArc {
            actor,
            _phantom: PhantomData,
        }
    }

    /// Find an actor by registered name and return its serialization
    pub fn encode<T: ActorEncode<S>, S: Serialize>(&self, name: &str) -> S {
        self.find::<T>(name).encode(self)
    }

    /// Attempt to process a message as directed by its `to` property. If the actor is not found, does not support the
    /// message, or failed to handle the message, send an error reply instead.
    pub(crate) fn handle_message(
        &self,
        msg: &Map<String, Value>,
        stream: &mut TcpStream,
        stream_id: StreamId,
    ) -> Result<(), ()> {
        let to = match msg.get("to") {