veecle_telemetry/
to_static.rs

1//! Utilities for converting borrowed data to owned data.
2//!
3//! This module provides traits and implementations for converting types with lifetime
4//! parameters to equivalent types with `'static` lifetime.
5//! This is essential for storing telemetry data in contexts that require owned data, such as when sending
6//! data across thread boundaries or storing it in global collectors.
7//!
8//! # Core Trait
9//!
10//! The [`ToStatic`] trait provides a standardized way to convert borrowed data to
11//! owned data while preserving the original structure and semantics.
12//!
13//! # Usage
14//!
15//! This is primarily used internally by the telemetry system to ensure that
16//! telemetry data can be safely stored and transmitted regardless of the original
17//! lifetime constraints of the input data.
18
19#[cfg(feature = "alloc")]
20use alloc::borrow::Cow;
21
22/// A trait for converting types with lifetime parameters to equivalent types with 'static lifetime.
23pub trait ToStatic: Clone {
24    /// The same type but with 'static lifetime and owned data.
25    type Static: 'static + Clone + Send + Sync;
26
27    /// Converts this type to the equivalent type with 'static lifetime.
28    ///
29    /// This method creates owned copies of any borrowed data, allowing the
30    /// resulting type to be used in contexts that require 'static lifetime.
31    fn to_static(&self) -> Self::Static;
32}
33
34#[cfg(feature = "alloc")]
35impl ToStatic for Cow<'_, str> {
36    type Static = Cow<'static, str>;
37
38    fn to_static(&self) -> Self::Static {
39        self.clone().into_owned().into()
40    }
41}