rocketmq_common/common/consistenthash/node.rs
1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Node trait for consistent hashing
16//!
17//! Represents a node that should be mapped to a hash ring.
18
19/// Trait representing a node in a consistent hash ring
20///
21/// Any type that implements this trait can be used as a node in consistent hashing.
22/// The key returned by `get_key()` will be used for hash mapping on the ring.
23///
24/// # Example
25/// ```
26/// use rocketmq_common::common::consistenthash::Node;
27///
28/// struct ServerNode {
29/// address: String,
30/// }
31///
32/// impl Node for ServerNode {
33/// fn get_key(&self) -> &str {
34/// &self.address
35/// }
36/// }
37///
38/// let node = ServerNode {
39/// address: "192.168.1.1:8080".to_string(),
40/// };
41/// assert_eq!(node.get_key(), "192.168.1.1:8080");
42/// ```
43pub trait Node {
44 /// Returns the key which will be used for hash mapping
45 ///
46 /// This key uniquely identifies the node in the hash ring and is used
47 /// to compute the node's position on the ring.
48 ///
49 /// # Returns
50 /// A string slice representing the node's key
51 fn get_key(&self) -> &str;
52}