pub struct ConnectionKey {
pub hostname: String,
pub plugin_name: String,
}Expand description
A unique identifier for a connection in the connection manager.
ConnectionKey serves as a composite key for looking up and managing connections
in the ConnectionManager. It combines a hostname with a connection plugin name to
uniquely identify a specific connection instance. This allows the same host to have
multiple concurrent connections handled by different plugins (e.g., SSH, NETCONF, HTTP).
The struct implements Hash and Eq to enable its use as a key in hash-based
collections like HashMap and DashMap.
§Hash Function Behavior
When inserting a ConnectionKey into a hash-based collection (like DashMap in
ConnectionManager), the hash function is used to:
-
Compute Hash Value: Both
hostnameandplugin_namefields are hashed together to produce a single hash value. This is done automatically by Rust’s derive macro forHash, which hashes each field in declaration order. -
Determine Bucket: The hash value is used to determine which internal bucket in the hash map should store this key-value pair. This enables O(1) average-case lookup performance.
-
Handle Collisions: If two different keys produce the same hash value (a hash collision), the
Eqimplementation is used to distinguish between them. The collection stores multiple entries in the same bucket and usesEqto find the exact match. -
Enable Deduplication: When inserting with the same
hostnameandplugin_name, the hash function ensures the key maps to the same bucket, andEqconfirms it’s the same key, allowing the collection to update the existing entry rather than creating a duplicate.
§Fields
hostname- The hostname or IP address of the target device. This identifies the remote endpoint for the connection.plugin_name- The connection plugin name (e.g., “ssh”, “netconf”, “http”). This distinguishes between different connection plugin types to the same host.
§Examples
§Basic Usage
let key = ConnectionKey::new("10.0.0.1", "ssh");
assert_eq!(key.hostname, "10.0.0.1");
assert_eq!(key.plugin_name, "ssh");§Multiple Connection Plugins per Host
use std::collections::HashMap;
let mut connections = HashMap::new();
let ssh_key = ConnectionKey::new("router1", "ssh");
let netconf_key = ConnectionKey::new("router1", "netconf");
// Same host can have different connection plugins
// Each key produces a different hash due to different plugin_name
connections.insert(ssh_key, "SSH connection");
connections.insert(netconf_key, "NETCONF connection");
assert_eq!(connections.len(), 2);§Key Equality and Deduplication
use std::collections::HashMap;
let mut connections = HashMap::new();
let key1 = ConnectionKey::new("router1", "ssh");
let key2 = ConnectionKey::new("router1", "ssh");
// Both keys have the same hostname and plugin_name
// They produce the same hash and are equal via Eq
connections.insert(key1, "First connection");
connections.insert(key2, "Second connection"); // Replaces first
assert_eq!(connections.len(), 1);
assert_eq!(connections.values().next(), Some(&"Second connection"));§Hash-Based Lookup in ConnectionManager
let manager = ConnectionManager::default();
let key = ConnectionKey::new("router1", "ssh");
// The hash function enables fast lookup:
// 1. Hash is computed from key
// 2. Hash determines which bucket to search
// 3. Eq is used to find exact match in bucket
if let Some(connection) = manager.get(&key) {
println!("Found existing connection");
}Fields§
§hostname: String§plugin_name: StringImplementations§
Source§impl ConnectionKey
impl ConnectionKey
Sourcepub fn new(hostname: impl Into<String>, plugin_name: impl Into<String>) -> Self
pub fn new(hostname: impl Into<String>, plugin_name: impl Into<String>) -> Self
Creates a new ConnectionKey from a hostname and plugin name.
This constructor provides a convenient way to create a connection key by accepting
any type that can be converted into a String for both the hostname and connection
type parameters. This allows passing &str, String, or other string-like types
without explicit conversion.
The resulting key uniquely identifies a connection in the ConnectionManager by
combining the target hostname with the connection plugin name.
§Parameters
hostname- The hostname or IP address of the target device. Accepts any type implementingInto<String>, such as&strorString. This identifies the remote endpoint for the connection.plugin_name- The connection plugin name (e.g., “ssh”, “netconf”, “http”). Accepts any type implementingInto<String>. This distinguishes between different connection plugin names to the same host.
§Returns
Returns a new ConnectionKey instance with the provided hostname and plugin name.
§Examples
// Using string slices
let key1 = ConnectionKey::new("10.0.0.1", "ssh");
// Using owned strings
let hostname = String::from("router1");
let plugin_name = String::from("netconf");
let key2 = ConnectionKey::new(hostname, plugin_name);
// Mixed types
let key3 = ConnectionKey::new("10.0.0.2", String::from("http"));Trait Implementations§
Source§impl Clone for ConnectionKey
impl Clone for ConnectionKey
Source§fn clone(&self) -> ConnectionKey
fn clone(&self) -> ConnectionKey
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ConnectionKey
impl Debug for ConnectionKey
impl Eq for ConnectionKey
Source§impl Hash for ConnectionKey
impl Hash for ConnectionKey
Source§impl PartialEq for ConnectionKey
impl PartialEq for ConnectionKey
Source§fn eq(&self, other: &ConnectionKey) -> bool
fn eq(&self, other: &ConnectionKey) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for ConnectionKey
Auto Trait Implementations§
impl Freeze for ConnectionKey
impl RefUnwindSafe for ConnectionKey
impl Send for ConnectionKey
impl Sync for ConnectionKey
impl Unpin for ConnectionKey
impl UnsafeUnpin for ConnectionKey
impl UnwindSafe for ConnectionKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.