Skip to main content

strand_withkey/
lib.rs

1// Copyright (C) The Strand-Braid Authors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Defines the WithKey trait for [Strand
5//! Camera](https://strawlab.org/strand-cam) and
6//! [Braid](https://strawlab.org/braid).
7
8#![warn(missing_docs)]
9
10/// A trait for types that can provide a key of type `T`.
11///
12/// This trait is used throughout the Strand Camera ecosystem to provide a
13/// consistent interface for objects that can be identified by a key. The key
14/// type `T` is generic, allowing for different key types such as frame numbers,
15/// timestamps, or custom identifiers.
16///
17/// # Type Parameters
18///
19/// * `T` - The type of the key that this object provides
20///
21/// # Examples
22///
23/// ```rust
24/// use strand_withkey::WithKey;
25///
26/// // A simple struct that uses a string as its key
27/// struct NamedItem {
28///     name: String,
29///     value: i32,
30/// }
31///
32/// impl WithKey<String> for NamedItem {
33///     fn key(&self) -> String {
34///         self.name.clone()
35///     }
36/// }
37///
38/// let item = NamedItem {
39///     name: "example".to_string(),
40///     value: 100,
41/// };
42/// assert_eq!(item.key(), "example");
43/// ```
44///
45/// ```rust
46/// use strand_withkey::WithKey;
47///
48/// // A struct that uses a numeric ID as its key
49/// struct NumberedFrame {
50///     frame_id: u64,
51///     timestamp: f64,
52/// }
53///
54/// impl WithKey<u64> for NumberedFrame {
55///     fn key(&self) -> u64 {
56///         self.frame_id
57///     }
58/// }
59///
60/// let frame = NumberedFrame {
61///     frame_id: 123,
62///     timestamp: 1234567890.0,
63/// };
64/// assert_eq!(frame.key(), 123);
65/// ```
66pub trait WithKey<T> {
67    /// Returns the key associated with this object.
68    ///
69    /// The key can be used to identify, index, or categorize this object
70    /// within collections or processing pipelines.
71    ///
72    /// # Returns
73    ///
74    /// The key of type `T` associated with this object.
75    fn key(&self) -> T;
76}