veecle_telemetry/types.rs
1//! Type definitions that adapt to different platform capabilities.
2//!
3//! This module provides type aliases and utilities that adapt to the current
4//! platform configuration.
5//! The types change behavior based on available features, allowing the same code to work efficiently in both `std` and
6//! `no_std` environments.
7//!
8//! # Platform Adaptation
9//!
10//! - **With `alloc`**: Uses `Cow<'_, str>` for strings and `Cow<'_, [T]>` for lists
11//! - **Without `alloc`**: Uses `&str` for strings and `&[T]` for lists
12//!
13//! This design allows for efficient zero-copy operation in `no_std` environments
14//! while providing flexibility for owned data when allocation is available.
15//!
16//! # Examples
17//!
18//! ```rust
19//! use veecle_telemetry::types::{ListType, StringType, list_from_slice};
20//!
21//! // StringType adapts to the platform
22//! let message: StringType = "Hello, world!".into();
23//!
24//! // ListType adapts to the platform
25//! let data = [1, 2, 3, 4, 5];
26//! let list: ListType<'_, i32> = list_from_slice(&data);
27//! ```
28
29#[cfg(feature = "alloc")]
30use alloc::borrow::Cow;
31
32/// A string type which changes depending on the platform.
33///
34/// When the `alloc` feature is enabled, this is `Cow<'a, str>` which can hold
35/// either borrowed or owned string data.
36/// When `alloc` is disabled, this is
37/// `&'a str` which only holds borrowed string data.
38#[cfg(feature = "alloc")]
39pub type StringType<'a> = Cow<'a, str>;
40
41/// A string type which changes depending on the platform.
42///
43/// When the `alloc` feature is enabled, this is `Cow<'a, str>` which can hold
44/// either borrowed or owned string data.
45/// When `alloc` is disabled, this is `&'a str` which only holds borrowed string data.
46#[cfg(not(feature = "alloc"))]
47pub type StringType<'a> = &'a str;
48
49/// A list type which changes depending on the platform.
50///
51/// When the `alloc` feature is enabled, this is `Cow<'a, [T]>` which can hold
52/// either borrowed or owned slice data.
53/// When `alloc` is disabled, this is `&'a [T]` which only holds borrowed slice data.
54#[cfg(feature = "alloc")]
55pub type ListType<'a, T> = Cow<'a, [T]>;
56
57/// A list type which changes depending on the platform.
58///
59/// When the `alloc` feature is enabled, this is `Cow<'a, [T]>` which can hold
60/// either borrowed or owned slice data. When `alloc` is disabled, this is
61/// `&'a [T]` which only holds borrowed slice data.
62#[cfg(not(feature = "alloc"))]
63pub type ListType<'a, T> = &'a [T];
64
65/// Converts a slice to the currently active [`ListType`].
66///
67/// This function adapts to the current platform configuration:
68/// - With `alloc`: Creates a `Cow::Borrowed` from the slice
69/// - Without `alloc`: Returns the slice directly
70///
71/// # Examples
72///
73/// ```rust
74/// use veecle_telemetry::types::{ListType, list_from_slice};
75///
76/// let data = [1, 2, 3, 4, 5];
77/// let list: ListType<'_, i32> = list_from_slice(&data);
78/// assert_eq!(list.len(), 5);
79/// ```
80pub fn list_from_slice<T>(slice: &[T]) -> ListType<'_, T>
81where
82 T: Clone,
83{
84 #[cfg(feature = "alloc")]
85 {
86 slice.into()
87 }
88 #[cfg(not(feature = "alloc"))]
89 {
90 slice
91 }
92}