From 019f12e3fc68678d1aa21734cbf4a17c030276ab Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Mar 2026 22:42:33 +0000 Subject: [PATCH] [refactor](trx-protocol): return Cow<'static, str> from mode_to_string Eliminates per-call String allocations for standard modes by returning borrowed static strings. Only the Other variant allocates. https://claude.ai/code/session_01XzurkeuUmamBuhQwxVy7T4 Signed-off-by: Claude --- src/trx-protocol/src/codec.rs | 35 ++++++++++++++++++--------------- src/trx-protocol/src/mapping.rs | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/trx-protocol/src/codec.rs b/src/trx-protocol/src/codec.rs index 9778eb4..6fba024 100644 --- a/src/trx-protocol/src/codec.rs +++ b/src/trx-protocol/src/codec.rs @@ -4,6 +4,8 @@ //! Codec utilities for parsing and formatting modes and envelopes. +use std::borrow::Cow; + use serde_json; use crate::types::{ClientCommand, ClientEnvelope}; @@ -33,23 +35,24 @@ pub fn parse_mode(s: &str) -> RigMode { /// Convert a RigMode back to its string representation. /// -/// This is the inverse of parse_mode. Standard modes return their uppercase names, -/// and Other variants return their inner string. -pub fn mode_to_string(mode: &RigMode) -> String { +/// This is the inverse of parse_mode. Standard modes return a borrowed +/// `&'static str` (zero allocation), while `Other` variants return the +/// owned inner string. +pub fn mode_to_string(mode: &RigMode) -> Cow<'static, str> { match mode { - RigMode::LSB => "LSB".to_string(), - RigMode::USB => "USB".to_string(), - RigMode::CW => "CW".to_string(), - RigMode::CWR => "CWR".to_string(), - RigMode::AM => "AM".to_string(), - RigMode::AMC => "AMC-QUAM".to_string(), - RigMode::FM => "FM".to_string(), - RigMode::WFM => "WFM".to_string(), - RigMode::AIS => "AIS".to_string(), - RigMode::VDES => "VDES".to_string(), - RigMode::DIG => "DIG".to_string(), - RigMode::PKT => "PKT".to_string(), - RigMode::Other(s) => s.clone(), + RigMode::LSB => Cow::Borrowed("LSB"), + RigMode::USB => Cow::Borrowed("USB"), + RigMode::CW => Cow::Borrowed("CW"), + RigMode::CWR => Cow::Borrowed("CWR"), + RigMode::AM => Cow::Borrowed("AM"), + RigMode::AMC => Cow::Borrowed("AMC-QUAM"), + RigMode::FM => Cow::Borrowed("FM"), + RigMode::WFM => Cow::Borrowed("WFM"), + RigMode::AIS => Cow::Borrowed("AIS"), + RigMode::VDES => Cow::Borrowed("VDES"), + RigMode::DIG => Cow::Borrowed("DIG"), + RigMode::PKT => Cow::Borrowed("PKT"), + RigMode::Other(s) => Cow::Owned(s.clone()), } } diff --git a/src/trx-protocol/src/mapping.rs b/src/trx-protocol/src/mapping.rs index c343b10..5c22e8b 100644 --- a/src/trx-protocol/src/mapping.rs +++ b/src/trx-protocol/src/mapping.rs @@ -87,7 +87,7 @@ pub fn rig_command_to_client(cmd: RigCommand) -> ClientCommand { RigCommand::SetFreq(freq) => ClientCommand::SetFreq { freq_hz: freq.hz }, RigCommand::SetCenterFreq(freq) => ClientCommand::SetCenterFreq { freq_hz: freq.hz }, RigCommand::SetMode(mode) => ClientCommand::SetMode { - mode: mode_to_string(&mode), + mode: mode_to_string(&mode).into_owned(), }, RigCommand::SetPtt(ptt) => ClientCommand::SetPtt { ptt }, RigCommand::PowerOn => ClientCommand::PowerOn,