wip improved trait shit

This commit is contained in:
2026-01-24 20:16:02 -07:00
committed by Liam Warfield
parent ff14f577fe
commit 411d923c2a
9 changed files with 118 additions and 181 deletions
+45 -96
View File
@@ -15,8 +15,7 @@ use std::time::Duration;
// ============================================================================
/// Trait for spawn handles that can be stored and used to spawn tasks later.
#[cfg(feature = "web")]
pub trait SpawnHandleTrait: Clone + 'static {
pub trait SpawnHandleInterface: Clone + 'static {
/// Spawn an async task using this handle.
fn spawn<F>(&self, future: F)
where
@@ -26,51 +25,39 @@ pub trait SpawnHandleTrait: Clone + 'static {
fn current() -> Self;
}
/// Trait for spawn handles that can be stored and used to spawn tasks later.
#[cfg(any(feature = "desktop", feature = "mobile"))]
pub trait SpawnHandleTrait: Clone + 'static {
/// Spawn an async task using this handle.
fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + Send + 'static;
/// Get a spawn handle for the current context.
fn current() -> Self;
pub trait AudioSystemInterface {
type AudioPlayer: AudioPlayerInterface;
}
/// Runtime primitives: task spawning and async sleep.
#[cfg(feature = "web")]
pub trait PlatformRuntime {
/// The spawn handle type for this platform.
type SpawnHandle: SpawnHandleTrait;
pub trait AudioPlayerInterface {}
/// Spawn an async task.
fn spawn<F>(future: F)
where
F: Future<Output = ()> + 'static;
/// This is the main trait that each platform must implement. It combines all
/// platform-specific functionality into a single interface, providing compile-time
/// verification that all platforms implement the required functionality.
pub trait PlatformInterface {
type AudioSystem: AudioSystemInterface;
type SpawnHandle: SpawnHandleInterface;
/// Async sleep for the given duration.
fn sleep(duration: Duration) -> impl Future<Output = ()>;
}
/// Initialize logging for the platform.
fn init_logging();
/// Runtime primitives: task spawning and async sleep.
#[cfg(any(feature = "desktop", feature = "mobile"))]
pub trait PlatformRuntime {
/// The spawn handle type for this platform.
type SpawnHandle: SpawnHandleTrait;
/// Request runtime permissions (Android audio recording, etc.).
fn request_permissions();
/// Spawn an async task.
fn spawn<F>(future: F)
where
F: Future<Output = ()> + Send + 'static;
/// Establish a connection to the Mumble server and run the network loop.
fn network_connect(
address: String,
username: String,
event_rx: &mut UnboundedReceiver<Command>,
gui_config: &ClientConfig,
) -> impl Future<Output = Result<(), Error>>;
/// Async sleep for the given duration.
fn sleep(duration: Duration) -> impl Future<Output = ()>;
}
/// Get server status (user count, version, etc.).
fn get_status(
client: &reqwest::Client,
) -> impl Future<Output = color_eyre::Result<ServerStatus>>;
/// Configuration persistence: loading and saving user preferences.
pub trait PlatformConfig {
/// Load the client configuration (proxy URL, cert hash, etc.).
/// Load the proxy overrides (proxy URL, cert hash, etc.).
fn load_config() -> impl Future<Output = color_eyre::Result<ClientConfig>>;
/// Load saved username.
@@ -84,39 +71,16 @@ pub trait PlatformConfig {
/// Save the default server URL.
fn set_default_server(server: &str) -> Option<()>;
/// Spawn an async task.
fn spawn<F>(future: F)
where
F: Future<Output = ()> + 'static;
/// Async sleep for the given duration.
fn sleep(duration: Duration) -> impl Future<Output = ()>;
}
/// Network operations: connecting to servers.
pub trait PlatformNetwork {
/// Establish a connection to the Mumble server and run the network loop.
fn network_connect(
address: String,
username: String,
event_rx: &mut UnboundedReceiver<Command>,
gui_config: &ClientConfig,
) -> impl Future<Output = Result<(), Error>>;
/// Get server status (user count, version, etc.).
fn get_status(client: &reqwest::Client)
-> impl Future<Output = color_eyre::Result<ServerStatus>>;
}
/// Platform initialization.
pub trait PlatformInit {
/// Initialize logging for the platform.
fn init_logging();
/// Request runtime permissions (Android audio recording, etc.).
fn request_permissions();
}
/// Combined platform trait.
///
/// This is the main trait that each platform must implement. It combines all
/// platform-specific functionality into a single interface, providing compile-time
/// verification that all platforms implement the required functionality.
pub trait Platform: PlatformRuntime + PlatformConfig + PlatformNetwork + PlatformInit {}
// ============================================================================
// Platform Modules
// ============================================================================
@@ -138,36 +102,21 @@ mod mobile;
// Platform Type Alias
// ============================================================================
/// The current platform type, selected at compile time based on features.
#[cfg(feature = "web")]
pub type CurrentPlatform = web::WebPlatform;
pub type Platform = web::WebPlatform;
#[cfg(feature = "desktop")]
pub type CurrentPlatform = desktop::DesktopPlatform;
#[cfg(all(feature = "desktop", not(feature = "web")))]
pub type Platform = desktop::DesktopPlatform;
#[cfg(feature = "mobile")]
pub type CurrentPlatform = mobile::MobilePlatform;
#[cfg(all(feature = "mobile", not(feature = "web"), not(feature = "desktop")))]
pub type Platform = mobile::MobilePlatform;
pub type AudioSystem = <Platform as PlatformInterface>::AudioSystem;
pub type AudioPlayer = <AudioSystem as AudioSystemInterface>::AudioPlayer;
pub type SpawnHandle = <Platform as PlatformInterface>::SpawnHandle;
/// Compile-time assertion that CurrentPlatform implements Platform.
const _: () = {
fn assert_platform<T: Platform>() {}
let _ = assert_platform::<CurrentPlatform>;
fn assert_platform<T: PlatformInterface>() {}
let _ = assert_platform::<Platform>;
};
// ============================================================================
// Platform Re-exports
// ============================================================================
#[cfg(feature = "desktop")]
pub use desktop::*;
#[cfg(feature = "mobile")]
pub use mobile::*;
#[cfg(feature = "mobile")]
pub use mobile::request_permissions;
#[cfg(any(feature = "desktop", feature = "web"))]
pub fn request_permissions() {}
#[cfg(all(feature = "web", not(any(feature = "desktop", feature = "mobile"))))]
pub use web::*;