Convert Block::instance
from a Box
to an Arc
This will reduce an allocation and deallocation everytime a message is sent
This commit is contained in:
parent
11f31d24ad
commit
c0de250953
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -501,6 +501,7 @@ dependencies = [
|
|||
"dbus-tokio",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"smart-default",
|
||||
"tokio",
|
||||
"uuid",
|
||||
]
|
||||
|
@ -558,6 +559,17 @@ version = "0.4.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
|
||||
|
||||
[[package]]
|
||||
name = "smart-default"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "133659a15339456eeeb07572eb02a91c91e9815e9cbc89566944d2c8d3efdbf6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.3.12"
|
||||
|
|
|
@ -11,5 +11,6 @@ dbus = "0.8"
|
|||
dbus-tokio = "0.5"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
smart-default = "0.6"
|
||||
tokio = { version = "0.2", features = ["full"] }
|
||||
uuid = { version = "0.8", features = [ "v4" ] }
|
||||
|
|
|
@ -25,17 +25,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
Arc::new(tiles::Load::new(
|
||||
0,
|
||||
sender.clone(),
|
||||
Uuid::new_v4().to_string().into_boxed_str(),
|
||||
Uuid::new_v4().to_string().into(),
|
||||
)),
|
||||
Arc::new(tiles::Hostname::new(
|
||||
1,
|
||||
sender.clone(),
|
||||
Uuid::new_v4().to_string().into_boxed_str(),
|
||||
Uuid::new_v4().to_string().into(),
|
||||
)),
|
||||
Arc::new(tiles::Time::new(
|
||||
2,
|
||||
sender,
|
||||
Uuid::new_v4().to_string().into_boxed_str(),
|
||||
Uuid::new_v4().to_string().into(),
|
||||
)),
|
||||
];
|
||||
|
||||
|
|
13
src/tile.rs
13
src/tile.rs
|
@ -1,6 +1,7 @@
|
|||
use serde::Serialize;
|
||||
use serde::{Serialize, ser::Serializer};
|
||||
use std::sync::Arc;
|
||||
use tokio::task::JoinHandle;
|
||||
use smart_default::SmartDefault;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
|
@ -29,7 +30,11 @@ impl Default for Markup {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Default, Debug)]
|
||||
fn arc_default<S>(arc: &Arc<str>, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
serializer.serialize_str(arc)
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Debug, SmartDefault)]
|
||||
pub struct Block {
|
||||
pub full_text: Box<str>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
|
@ -53,7 +58,9 @@ pub struct Block {
|
|||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub align: Option<Alignment>,
|
||||
pub name: Box<str>,
|
||||
pub instance: Box<str>,
|
||||
#[serde(serialize_with = "arc_default")]
|
||||
#[default = r#""".into()"#]
|
||||
pub instance: Arc<str>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub urgent: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
|
|
|
@ -9,11 +9,11 @@ use tokio::task::JoinHandle;
|
|||
pub struct Hostname {
|
||||
sender_id: usize,
|
||||
sender: RwLock<Sender<TileData>>,
|
||||
instance: Box<str>,
|
||||
instance: Arc<str>,
|
||||
}
|
||||
|
||||
impl Hostname {
|
||||
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Hostname {
|
||||
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Hostname {
|
||||
Hostname {
|
||||
sender_id,
|
||||
sender: RwLock::new(sender),
|
||||
|
|
|
@ -11,11 +11,11 @@ use tokio::time::interval;
|
|||
pub struct Load {
|
||||
sender_id: usize,
|
||||
sender: RwLock<Sender<TileData>>,
|
||||
instance: Box<str>,
|
||||
instance: Arc<str>,
|
||||
}
|
||||
|
||||
impl Load {
|
||||
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Load {
|
||||
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Load {
|
||||
Load {
|
||||
sender_id,
|
||||
sender: RwLock::new(sender),
|
||||
|
|
|
@ -11,13 +11,13 @@ use tokio::time::delay_for;
|
|||
pub struct Time {
|
||||
sender_id: usize,
|
||||
sender: RwLock<Sender<TileData>>,
|
||||
instance: Box<str>,
|
||||
instance: Arc<str>,
|
||||
format: Box<str>,
|
||||
short_format: Box<str>,
|
||||
}
|
||||
|
||||
impl Time {
|
||||
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Box<str>) -> Time {
|
||||
pub fn new(sender_id: usize, sender: Sender<TileData>, instance: Arc<str>) -> Time {
|
||||
Time {
|
||||
sender_id,
|
||||
sender: RwLock::new(sender),
|
||||
|
|
Loading…
Reference in a new issue