Skip to content

Commit a8b8fba

Browse files
committed
Remove heap allocations from SshId
1 parent da2c489 commit a8b8fba

4 files changed

Lines changed: 21 additions & 14 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;
@@ -1698,11 +1699,12 @@ pub struct Config {
16981699
impl Default for Config {
16991700
fn default() -> Config {
17001701
Config {
1701-
client_id: SshId::Standard(format!(
1702-
"SSH-2.0-{}_{}",
1702+
client_id: SshId::Standard(Cow::Borrowed(concat!(
1703+
"SSH-2.0-",
17031704
env!("CARGO_PKG_NAME"),
1705+
"_",
17041706
env!("CARGO_PKG_VERSION")
1705-
)),
1707+
))),
17061708
limits: Limits::default(),
17071709
window_size: 2097152,
17081710
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: 11 additions & 7 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 {
@@ -41,7 +42,10 @@ impl SshId {
4142

4243
pub(crate) fn write(&self, buffer: &mut CryptoVec) {
4344
match self {
44-
Self::Standard(s) => buffer.extend(format!("{s}\r\n").as_bytes()),
45+
Self::Standard(s) => {
46+
buffer.extend(s.as_bytes());
47+
buffer.extend(b"\r\n");
48+
},
4549
Self::Raw(s) => buffer.extend(s.as_bytes()),
4650
}
4751
}
@@ -50,19 +54,19 @@ impl SshId {
5054
#[test]
5155
fn test_ssh_id() {
5256
let mut buffer = CryptoVec::new();
53-
SshId::Standard("SSH-2.0-acme".to_string()).write(&mut buffer);
57+
SshId::Standard("SSH-2.0-acme".into()).write(&mut buffer);
5458
assert_eq!(&buffer[..], b"SSH-2.0-acme\r\n");
5559

5660
let mut buffer = CryptoVec::new();
57-
SshId::Raw("SSH-2.0-raw\n".to_string()).write(&mut buffer);
61+
SshId::Raw("SSH-2.0-raw\n".into()).write(&mut buffer);
5862
assert_eq!(&buffer[..], b"SSH-2.0-raw\n");
5963

6064
assert_eq!(
61-
SshId::Standard("SSH-2.0-acme".to_string()).as_kex_hash_bytes(),
65+
SshId::Standard("SSH-2.0-acme".into()).as_kex_hash_bytes(),
6266
b"SSH-2.0-acme"
6367
);
6468
assert_eq!(
65-
SshId::Raw("SSH-2.0-raw\n".to_string()).as_kex_hash_bytes(),
69+
SshId::Raw("SSH-2.0-raw\n".into()).as_kex_hash_bytes(),
6670
b"SSH-2.0-raw"
6771
);
6872
}

0 commit comments

Comments
 (0)