Skip to content

Commit

Permalink
Reducing String use when its not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
GPeaky committed Oct 13, 2024
1 parent 2d7740e commit eaaaa17
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion crates/db/src/cache/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{EntityCache, CACHE_CAPACITY};
pub struct ChampionshipCache {
inner: Cache<i32, Arc<Championship>>,
races: Cache<i32, Vec<Arc<Race>>>,
name_to_id: Cache<String, i32>,
name_to_id: Cache<Box<str>, i32>,
user_championships: Cache<i32, Vec<Arc<Championship>>>,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/cache/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use entities::SharedDriver;
use super::CACHE_CAPACITY;

pub struct DriverCache {
inner: Cache<String, SharedDriver>,
inner: Cache<Box<str>, SharedDriver>,
}

impl DriverCache {
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/cache/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{EntityCache, CACHE_CAPACITY};

pub struct UserCache {
inner: Cache<i32, SharedUser>,
email_to_id: Cache<String, i32>,
email_to_id: Cache<Box<str>, i32>,
}

impl UserCache {
Expand Down
2 changes: 1 addition & 1 deletion crates/entities/src/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct ChampionshipRelation {
pub struct Championship {
pub id: i32,
pub port: i32,
pub name: String,
pub name: Box<str>,
#[serde(skip_serializing)]
pub owner_id: i32,
pub category: Category,
Expand Down
2 changes: 1 addition & 1 deletion crates/entities/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub type SharedDriver = Arc<Driver>;
/// Represents a driver in the championship
#[allow(unused)]
pub struct Driver {
pub steam_name: String,
pub steam_name: Box<str>,
pub nationality: i16,
pub user_id: Option<i32>,
pub created_at: DateTime<Utc>,
Expand Down
8 changes: 4 additions & 4 deletions crates/entities/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ pub enum Role {
#[derive(Debug, Serialize)]
pub struct User {
pub id: i32,
pub email: String,
pub username: String,
pub email: Box<str>,
pub username: Box<str>,
#[serde(skip_serializing)]
pub password: Option<String>,
pub avatar: String,
pub password: Option<Box<str>>,
pub avatar: Box<str>,
#[serde(skip_serializing)]
pub provider: Provider,
pub role: Role,
Expand Down
2 changes: 1 addition & 1 deletion crates/intelli-core/src/repositories/championship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl ChampionshipRepository {
}
}

pub async fn drivers_linked(&self, id: i32) -> AppResult<Vec<String>> {
pub async fn drivers_linked(&self, id: i32) -> AppResult<Vec<Box<str>>> {
let stream = {
let conn = self.db.pg.get().await?;

Expand Down
2 changes: 1 addition & 1 deletion crates/intelli-core/src/repositories/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl UserRepository {
/// # Returns
/// Boolean indicating if the password is valid.
#[inline]
pub async fn validate_password(&self, pwd: String, hash: String) -> AppResult<bool> {
pub async fn validate_password(&self, pwd: String, hash: Box<str>) -> AppResult<bool> {
self.password_hasher.verify_password(hash, pwd).await
}
}
2 changes: 1 addition & 1 deletion crates/intelli-core/src/services/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl EmailService {
Address::from_str(&dotenvy::var("EMAIL_FROM").unwrap()).unwrap(),
))
.to(Mailbox::new(
Some(user.username.to_owned()),
Some(user.username.to_string()),
Address::from_str(&user.email).unwrap(),
))
.subject(subject)
Expand Down
2 changes: 1 addition & 1 deletion crates/password-hash/src/password_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl PasswordHasher {
.unwrap_or_else(|_| Err(CommonError::InternalServerError)?)
}

pub async fn verify_password(&self, encoded: String, password: String) -> AppResult<bool> {
pub async fn verify_password(&self, encoded: Box<str>, password: String) -> AppResult<bool> {
let _permit = self.semaphore.acquire().await.unwrap();

tokio::task::spawn_blocking(move || {
Expand Down
30 changes: 15 additions & 15 deletions crates/telemetry/src/firewall.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use ahash::AHashMap;
use error::{AppResult, FirewallError};
use regex::Regex;
use std::{str, sync::Arc};
use std::{net::IpAddr, str, sync::Arc};
use tokio::process::Command;
use tokio::sync::RwLock;
use tracing::{error, warn};

/// Represents a single firewall rule.
struct FirewallRule {
port: u16,
handle: String,
ip_address: Option<String>,
handle: Box<str>,
ip_address: Option<IpAddr>,
}

impl FirewallRule {
Expand All @@ -19,7 +19,7 @@ impl FirewallRule {
/// # Arguments
/// - `port`: The port number for the rule.
/// - `handle`: A unique identifier for the rule.
pub fn new(port: u16, handle: String) -> Self {
pub fn new(port: u16, handle: Box<str>) -> Self {
FirewallRule {
port,
handle,
Expand Down Expand Up @@ -71,7 +71,7 @@ impl FirewallService {
&port.to_string(),
"accept",
])
.await?;
.await?;

let ruleset = Self::ruleset().await?;
let handle =
Expand All @@ -91,7 +91,7 @@ impl FirewallService {
///
/// # Returns
/// Result indicating success or failure.
pub async fn restrict_to_ip(&self, id: i32, ip_address: String) -> AppResult<()> {
pub async fn restrict_to_ip(&self, id: i32, ip_address: IpAddr) -> AppResult<()> {
if cfg!(not(target_os = "linux")) {
warn!("Firewall not supported on this platform");
return Ok(());
Expand All @@ -109,7 +109,7 @@ impl FirewallService {
"handle",
&rule.handle,
])
.await?;
.await?;

Self::nft_command(&[
"add",
Expand All @@ -119,13 +119,13 @@ impl FirewallService {
"allow",
"ip",
"saddr",
&ip_address,
&ip_address.to_string(),
"udp",
"dport",
&rule.port.to_string(),
"accept",
])
.await?;
.await?;

let ruleset = Self::ruleset().await?;
let new_handle = Self::extract_handle_from_ruleset(
Expand Down Expand Up @@ -168,7 +168,7 @@ impl FirewallService {
"handle",
&rule.handle,
])
.await?;
.await?;

rules.remove(&id);
Ok(())
Expand Down Expand Up @@ -214,9 +214,9 @@ impl FirewallService {
/// Retrieves the current firewall ruleset.
///
/// # Returns
/// String representation of the current ruleset or an error.
/// Box<str> representation of the current ruleset or an error.
#[inline]
async fn ruleset() -> AppResult<String> {
async fn ruleset() -> AppResult<Box<str>> {
let output = Command::new("nft")
.args(["-a", "list", "ruleset"])
.output()
Expand All @@ -227,7 +227,7 @@ impl FirewallService {
Err(FirewallError::ExecutionError)?
}

let ruleset = String::from_utf8(output.stdout).unwrap_or_default();
let ruleset = Box::from(str::from_utf8(&output.stdout).unwrap_or_default());
Ok(ruleset)
}

Expand Down Expand Up @@ -263,13 +263,13 @@ impl FirewallService {
/// # Returns
/// The extracted handle as a string or an error.
#[inline]
fn extract_handle_from_ruleset(ruleset: &str, search_pattern: &str) -> AppResult<String> {
fn extract_handle_from_ruleset(ruleset: &str, search_pattern: &str) -> AppResult<Box<str>> {
let pattern = format!(r"{}\s+#\s+handle\s+(\d+)", regex::escape(search_pattern));
let re = Regex::new(&pattern).map_err(|_| FirewallError::ParseError)?;

if let Some(caps) = re.captures(ruleset) {
if let Some(handle) = caps.get(1) {
return Ok(handle.as_str().to_string());
return Ok(Box::from(handle.as_str()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/telemetry/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl F1Service {
if self
.f1_state
.firewall
.restrict_to_ip(self.championship_id, address.ip().to_string())
.restrict_to_ip(self.championship_id, address.ip())
.await
.is_err()
{
Expand Down Expand Up @@ -474,7 +474,7 @@ impl F1Service {
}

if drivers
.binary_search_by(|probe| probe.as_str().cmp(steam_name))
.binary_search_by(|probe| probe.as_ref().cmp(steam_name))
.is_err()
{
self.f1_state
Expand Down

0 comments on commit eaaaa17

Please sign in to comment.