Skip to content

Commit 6f70150

Browse files
kpcyrdEugeny
andauthored
Remove heap allocations from SshId (#656)
Co-authored-by: Eugene <inbox@null.page>
1 parent e75de5a commit 6f70150

4 files changed

Lines changed: 17 additions & 13 deletions

File tree

russh/src/client/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//!
3535
//! [Session]: client::Session
3636
37+
use std::borrow::Cow;
3738
use std::collections::{HashMap, VecDeque};
3839
use std::convert::TryInto;
3940
use std::num::Wrapping;
@@ -1701,11 +1702,12 @@ pub struct Config {
17011702
impl Default for Config {
17021703
fn default() -> Config {
17031704
Config {
1704-
client_id: SshId::Standard(format!(
1705-
"SSH-2.0-{}_{}",
1705+
client_id: SshId::Standard(Cow::Borrowed(concat!(
1706+
"SSH-2.0-",
17061707
env!("CARGO_PKG_NAME"),
1708+
"_",
17071709
env!("CARGO_PKG_VERSION")
1708-
)),
1710+
))),
17091711
limits: Limits::default(),
17101712
window_size: 2097152,
17111713
maximum_packet_size: 32768,

russh/src/client/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ mod tests {
8787
// Configure the server
8888
let mut config = server::Config::default();
8989
config.auth_rejection_time = std::time::Duration::from_secs(1);
90-
config.server_id = SshId::Standard("SSH-1.99-CustomServer_1.0".to_string());
90+
config.server_id = SshId::Standard("SSH-1.99-CustomServer_1.0".into());
9191
config.inactivity_timeout = None;
9292
config
9393
.keys

russh/src/server/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ pub struct Config {
102102
impl Default for Config {
103103
fn default() -> Config {
104104
Config {
105-
server_id: SshId::Standard(format!(
106-
"SSH-2.0-{}_{}",
105+
server_id: SshId::Standard(Cow::Borrowed(concat!(
106+
"SSH-2.0-",
107107
env!("CARGO_PKG_NAME"),
108+
"_",
108109
env!("CARGO_PKG_VERSION")
109-
)),
110+
))),
110111
methods: auth::MethodSet::all(),
111112
auth_rejection_time: std::time::Duration::from_secs(1),
112113
auth_rejection_time_initial: None,

russh/src/sshbuffer.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515

1616
use core::fmt;
17+
use std::borrow::Cow;
1718
use std::num::Wrapping;
1819

1920
use cipher::SealingKey;
@@ -26,9 +27,9 @@ use super::*;
2627
#[derive(Debug)]
2728
pub enum SshId {
2829
/// When sending the id, append RFC standard `\r\n`. Example: `SshId::Standard("SSH-2.0-acme")`
29-
Standard(String),
30+
Standard(Cow<'static, str>),
3031
/// When sending the id, use this buffer as it is and do not append additional line terminators.
31-
Raw(String),
32+
Raw(Cow<'static, str>),
3233
}
3334

3435
impl SshId {
@@ -52,19 +53,19 @@ impl SshId {
5253
#[test]
5354
fn test_ssh_id() {
5455
let mut buffer = Vec::new();
55-
SshId::Standard("SSH-2.0-acme".to_string()).write(&mut buffer);
56+
SshId::Standard("SSH-2.0-acme".into()).write(&mut buffer);
5657
assert_eq!(&buffer[..], b"SSH-2.0-acme\r\n");
5758

5859
let mut buffer = Vec::new();
59-
SshId::Raw("SSH-2.0-raw\n".to_string()).write(&mut buffer);
60+
SshId::Raw("SSH-2.0-raw\n".into()).write(&mut buffer);
6061
assert_eq!(&buffer[..], b"SSH-2.0-raw\n");
6162

6263
assert_eq!(
63-
SshId::Standard("SSH-2.0-acme".to_string()).as_kex_hash_bytes(),
64+
SshId::Standard("SSH-2.0-acme".into()).as_kex_hash_bytes(),
6465
b"SSH-2.0-acme"
6566
);
6667
assert_eq!(
67-
SshId::Raw("SSH-2.0-raw\n".to_string()).as_kex_hash_bytes(),
68+
SshId::Raw("SSH-2.0-raw\n".into()).as_kex_hash_bytes(),
6869
b"SSH-2.0-raw"
6970
);
7071
}

0 commit comments

Comments
 (0)