pitwall-tauri 0.1.0

Tauri integration for Pitwall telemetry library
Documentation
//! Stream-to-Channel Bridge
//!
//! Provides a simple, stateless bridge between Pitwall's async stream API
//! and Tauri's IPC channel system for high-frequency telemetry streaming.

use futures::Stream;
use futures::StreamExt;

/// Bridge a Pitwall stream to a Tauri IPC channel.
///
/// This function provides a stateless, zero-overhead bridge between Pitwall's
/// tokio-based async streams and Tauri's IPC channel system. It's designed for
/// high-frequency telemetry streaming (60Hz+) with minimal latency.
///
/// # Arguments
///
/// * `stream` - Any Pitwall stream (telemetry frames, session updates, etc.)
/// * `channel` - Tauri IPC channel for sending data to the frontend
///
/// # Returns
///
/// Returns `Ok(())` when the stream ends naturally, or an error if the channel
/// fails to send data.
///
/// # Example
///
/// ```rust,ignore
/// use pitwall::Pitwall;
/// use pitwall_tauri::to_channel;
/// use tauri::ipc::Channel;
///
/// #[tauri::command]
/// async fn start_telemetry(telemetry_channel: Channel<MyFrame>) {
///     let connection = Pitwall::connect().await?;
///     let stream = connection.subscribe::<MyFrame>(UpdateRate::Native);
///
///     // Spawn the bridge task
///     tokio::spawn(async move {
///         to_channel(stream, telemetry_channel).await
///     });
/// }
/// ```
///
/// # Performance
///
/// This bridge adds negligible overhead (~microseconds per frame) by:
/// - Using async/await for efficient task scheduling
/// - Avoiding unnecessary allocations
/// - Leveraging Tauri's optimized IPC serialization
///
pub async fn to_channel<T>(
    mut stream: impl Stream<Item = T> + Unpin,
    channel: tauri::ipc::Channel<T>,
) -> Result<(), Box<dyn std::error::Error>>
where
    T: serde::Serialize,
{
    while let Some(item) = stream.next().await {
        channel.send(item)?;
    }
    Ok(())
}