Skip to main content

ConnectionKey

Struct ConnectionKey 

Source
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:

  1. Compute Hash Value: Both hostname and plugin_name fields are hashed together to produce a single hash value. This is done automatically by Rust’s derive macro for Hash, which hashes each field in declaration order.

  2. 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.

  3. Handle Collisions: If two different keys produce the same hash value (a hash collision), the Eq implementation is used to distinguish between them. The collection stores multiple entries in the same bucket and uses Eq to find the exact match.

  4. Enable Deduplication: When inserting with the same hostname and plugin_name, the hash function ensures the key maps to the same bucket, and Eq confirms 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: String

Implementations§

Source§

impl ConnectionKey

Source

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 implementing Into<String>, such as &str or String. This identifies the remote endpoint for the connection.
  • plugin_name - The connection plugin name (e.g., “ssh”, “netconf”, “http”). Accepts any type implementing Into<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

Source§

fn clone(&self) -> ConnectionKey

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ConnectionKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for ConnectionKey

Source§

impl Hash for ConnectionKey

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ConnectionKey

Source§

fn eq(&self, other: &ConnectionKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ConnectionKey

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.