Skip to content

Commit 42fa2a4

Browse files
committed
Use Clone trait instead of Default
Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent e795e07 commit 42fa2a4

File tree

4 files changed

+15
-87
lines changed

4 files changed

+15
-87
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ssh_agent_lib::agent::{Session, listen};
2222
use ssh_agent_lib::proto::{Identity, SignRequest};
2323
use ssh_key::{Algorithm, Signature};
2424
25-
#[derive(Default)]
25+
#[derive(Default, Clone)]
2626
struct MyAgent;
2727
2828
#[ssh_agent_lib::async_trait]

examples/key_storage.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rsa::BigUint;
99
use sha1::Sha1;
1010
#[cfg(windows)]
1111
use ssh_agent_lib::agent::NamedPipeListener as Listener;
12-
use ssh_agent_lib::agent::{listen, Agent, Session};
12+
use ssh_agent_lib::agent::{listen, Session};
1313
use ssh_agent_lib::error::AgentError;
1414
use ssh_agent_lib::proto::extension::{QueryResponse, RestrictDestination, SessionBind};
1515
use ssh_agent_lib::proto::{
@@ -31,6 +31,7 @@ struct Identity {
3131
comment: String,
3232
}
3333

34+
#[derive(Default, Clone)]
3435
struct KeyStorage {
3536
identities: Arc<Mutex<Vec<Identity>>>,
3637
}
@@ -224,39 +225,6 @@ impl Session for KeyStorage {
224225
}
225226
}
226227

227-
struct KeyStorageAgent {
228-
identities: Arc<Mutex<Vec<Identity>>>,
229-
}
230-
231-
impl KeyStorageAgent {
232-
fn new() -> Self {
233-
Self {
234-
identities: Arc::new(Mutex::new(vec![])),
235-
}
236-
}
237-
}
238-
239-
#[cfg(unix)]
240-
impl Agent<Listener> for KeyStorageAgent {
241-
fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session {
242-
KeyStorage {
243-
identities: Arc::clone(&self.identities),
244-
}
245-
}
246-
}
247-
248-
#[cfg(windows)]
249-
impl Agent<Listener> for KeyStorageAgent {
250-
fn new_session(
251-
&mut self,
252-
_socket: &tokio::net::windows::named_pipe::NamedPipeServer,
253-
) -> impl Session {
254-
KeyStorage {
255-
identities: Arc::clone(&self.identities),
256-
}
257-
}
258-
}
259-
260228
#[tokio::main]
261229
async fn main() -> Result<(), AgentError> {
262230
env_logger::init();
@@ -272,6 +240,6 @@ async fn main() -> Result<(), AgentError> {
272240
#[cfg(windows)]
273241
std::fs::File::create("server-started")?;
274242

275-
listen(Listener::bind(socket)?, KeyStorageAgent::new()).await?;
243+
listen(Listener::bind(socket)?, KeyStorage::default()).await?;
276244
Ok(())
277245
}

examples/openpgp-card-agent.rs

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ use openpgp_card::{
2626
use retainer::{Cache, CacheExpiration};
2727
use secrecy::{ExposeSecret, SecretString};
2828
use service_binding::Binding;
29-
#[cfg(windows)]
30-
use ssh_agent_lib::agent::NamedPipeListener as Listener;
3129
use ssh_agent_lib::{
32-
agent::{bind, Agent, Session},
30+
agent::{bind, Session},
3331
error::AgentError,
3432
proto::{AddSmartcardKeyConstrained, Identity, KeyConstraint, SignRequest, SmartcardKey},
3533
};
@@ -38,58 +36,20 @@ use ssh_key::{
3836
Algorithm, Signature,
3937
};
4038
use testresult::TestResult;
41-
use tokio::net::TcpListener;
42-
#[cfg(not(windows))]
43-
use tokio::net::UnixListener as Listener;
4439

45-
struct CardAgent {
40+
#[derive(Clone)]
41+
struct CardSession {
4642
pwds: Arc<Cache<String, SecretString>>,
4743
}
4844

49-
impl CardAgent {
45+
impl CardSession {
5046
pub fn new() -> Self {
5147
let pwds: Arc<Cache<String, SecretString>> = Arc::new(Default::default());
5248
let clone = Arc::clone(&pwds);
5349
tokio::spawn(async move { clone.monitor(4, 0.25, Duration::from_secs(3)).await });
5450
Self { pwds }
5551
}
56-
}
57-
58-
#[cfg(unix)]
59-
impl Agent<Listener> for CardAgent {
60-
fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session {
61-
CardSession {
62-
pwds: Arc::clone(&self.pwds),
63-
}
64-
}
65-
}
66-
67-
#[cfg(unix)]
68-
impl Agent<TcpListener> for CardAgent {
69-
fn new_session(&mut self, _socket: &tokio::net::TcpStream) -> impl Session {
70-
CardSession {
71-
pwds: Arc::clone(&self.pwds),
72-
}
73-
}
74-
}
7552

76-
#[cfg(windows)]
77-
impl Agent<Listener> for CardAgent {
78-
fn new_session(
79-
&mut self,
80-
_socket: &tokio::net::windows::named_pipe::NamedPipeServer,
81-
) -> impl Session {
82-
CardSession {
83-
pwds: Arc::clone(&self.pwds),
84-
}
85-
}
86-
}
87-
88-
struct CardSession {
89-
pwds: Arc<Cache<String, SecretString>>,
90-
}
91-
92-
impl CardSession {
9353
async fn handle_sign(
9454
&self,
9555
request: SignRequest,
@@ -227,6 +187,6 @@ async fn main() -> TestResult {
227187
env_logger::init();
228188

229189
let args = Args::parse();
230-
bind(args.host.try_into()?, CardAgent::new()).await?;
190+
bind(args.host.try_into()?, CardSession::new()).await?;
231191
Ok(())
232192
}

src/agent.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,32 +287,32 @@ where
287287
#[cfg(unix)]
288288
impl<T> Agent<tokio::net::UnixListener> for T
289289
where
290-
T: Default + Send + Sync + Session,
290+
T: Clone + Send + Sync + Session,
291291
{
292292
fn new_session(&mut self, _socket: &tokio::net::UnixStream) -> impl Session {
293-
Self::default()
293+
Self::clone(self)
294294
}
295295
}
296296

297297
impl<T> Agent<tokio::net::TcpListener> for T
298298
where
299-
T: Default + Send + Sync + Session,
299+
T: Clone + Send + Sync + Session,
300300
{
301301
fn new_session(&mut self, _socket: &tokio::net::TcpStream) -> impl Session {
302-
Self::default()
302+
Self::clone(self)
303303
}
304304
}
305305

306306
#[cfg(windows)]
307307
impl<T> Agent<NamedPipeListener> for T
308308
where
309-
T: Default + Send + Sync + Session,
309+
T: Clone + Send + Sync + Session,
310310
{
311311
fn new_session(
312312
&mut self,
313313
_socket: &tokio::net::windows::named_pipe::NamedPipeServer,
314314
) -> impl Session {
315-
Self::default()
315+
Self::clone(self)
316316
}
317317
}
318318

0 commit comments

Comments
 (0)