mirror of
https://github.com/servo/servo
synced 2026-05-12 01:46:28 +02:00
Replace the previously hardcoded "name prefix" strings for actors with [`type_name`](https://doc.rust-lang.org/std/any/fn.type_name.html). Note that we aren't using the prefix directly as an unique identifier, and `type_name` wouldn't be suitable for that. The suffix is an incremental counter shared across all actors, so that alone is enough to differentiate them. The purpose of the prefix is to visually inspect the logs and see what actors are involved. With this change the prefix will be slightly different: "InspectorActor11" vs "inspector11", but that shouldn't affect behaviour. The eventual goal is to remove `new_name` and force the use of `register/register_later` to create an actor. This is however a bit more complicated, see #41768. While I'd love to add `base_name` directly to the `Actor` trait, that would unfortunately mean that it wouldn't be [dyn compatible](https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility). There are workarounds for this like separating the trait in two and implementing it automatically, but I feel that would be too convoluted. Testing: Manual testing, this patch shouldn't change behaviour. Part of: #41768 Signed-off-by: eri <eri@igalia.com>
88 lines
2.4 KiB
Rust
88 lines
2.4 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
use serde::Serialize;
|
|
use serde_json::{Map, Value};
|
|
|
|
use crate::StreamId;
|
|
use crate::actor::{Actor, ActorError, ActorRegistry};
|
|
use crate::protocol::ClientRequest;
|
|
|
|
const INITIAL_LENGTH: usize = 500;
|
|
|
|
pub struct LongStringActor {
|
|
name: String,
|
|
full_string: String,
|
|
}
|
|
|
|
#[derive(Clone, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct LongStringObj {
|
|
#[serde(rename = "type")]
|
|
type_: String,
|
|
actor: String,
|
|
length: usize,
|
|
initial: String,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct SubstringReply {
|
|
from: String,
|
|
substring: String,
|
|
}
|
|
|
|
impl Actor for LongStringActor {
|
|
fn name(&self) -> String {
|
|
self.name.clone()
|
|
}
|
|
|
|
fn handle_message(
|
|
&self,
|
|
request: ClientRequest,
|
|
_registry: &ActorRegistry,
|
|
msg_type: &str,
|
|
msg: &Map<String, Value>,
|
|
_id: StreamId,
|
|
) -> Result<(), ActorError> {
|
|
match msg_type {
|
|
"substring" => {
|
|
let start = msg.get("start").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
|
|
let end = msg
|
|
.get("end")
|
|
.and_then(|v| v.as_u64())
|
|
.unwrap_or(self.full_string.len() as u64) as usize;
|
|
let substring: String = self
|
|
.full_string
|
|
.chars()
|
|
.skip(start)
|
|
.take(end - start)
|
|
.collect();
|
|
let reply = SubstringReply {
|
|
from: self.name(),
|
|
substring,
|
|
};
|
|
request.reply_final(&reply)?
|
|
},
|
|
_ => return Err(ActorError::UnrecognizedPacketType),
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl LongStringActor {
|
|
pub fn new(registry: &ActorRegistry, full_string: String) -> Self {
|
|
let name = registry.new_name::<Self>();
|
|
LongStringActor { name, full_string }
|
|
}
|
|
|
|
pub fn long_string_obj(&self) -> LongStringObj {
|
|
LongStringObj {
|
|
type_: "longString".to_string(),
|
|
actor: self.name.clone(),
|
|
length: self.full_string.len(),
|
|
initial: self.full_string.chars().take(INITIAL_LENGTH).collect(),
|
|
}
|
|
}
|
|
}
|