Skip to content

Commit 0ac8b27

Browse files
committed
Add doc example to agent::Agent
Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent c55b189 commit 0ac8b27

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/agent.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,37 @@ where
251251
}
252252

253253
/// Factory of sessions for the given type of sockets.
254+
///
255+
/// An agent implementation is automatically created for types which
256+
/// implement [`Session`] and [`Clone`]: new sessions are created by
257+
/// cloning the agent object. This is usually sufficient for the
258+
/// majority of use cases. In case the information about the
259+
/// underlying socket (connection source) is needed the [`Agent`] can
260+
/// be implemented manually.
261+
///
262+
/// # Examples
263+
///
264+
/// This example shows how to retrieve the connecting process ID on Unix:
265+
///
266+
/// ```
267+
/// use ssh_agent_lib::agent::{Agent, Session};
268+
///
269+
/// #[derive(Debug, Default)]
270+
/// struct AgentSocketInfo;
271+
///
272+
/// #[cfg(unix)]
273+
/// impl Agent<tokio::net::UnixListener> for AgentSocketInfo {
274+
/// fn new_session(&mut self, socket: &tokio::net::UnixStream) -> impl Session {
275+
/// let _socket_info = format!(
276+
/// "unix: addr: {:?} cred: {:?}",
277+
/// socket.peer_addr().unwrap(),
278+
/// socket.peer_cred().unwrap()
279+
/// );
280+
/// Self
281+
/// }
282+
/// }
283+
/// # impl Session for AgentSocketInfo { }
284+
/// ```
254285
pub trait Agent<S>: 'static + Send + Sync
255286
where
256287
S: ListeningSocket + fmt::Debug + Send,

0 commit comments

Comments
 (0)