dioxus_provider/
types.rs

1//! Common types and trait bounds used throughout dioxus-provider
2//!
3//! This module defines the foundational trait bounds that enable dioxus-provider to work
4//! seamlessly across different platforms (web, desktop, mobile) while maintaining type safety.
5
6/// Common trait bounds for provider parameters
7///
8/// Provider parameters must satisfy these bounds to enable:
9/// - **Clone**: Parameters are cloned when passed to async functions and stored in cache keys
10/// - **PartialEq**: Required for comparing parameters to determine cache key equality
11/// - **Hash**: Enables efficient cache key generation from parameter values
12/// - **Debug**: Provides better error messages and logging output
13/// - **Send + Sync**: Allows parameters to be safely shared across async contexts
14/// - **'static**: Ensures parameters can be stored in global caches and async tasks
15///
16/// ## Example
17///
18/// ```rust
19/// // Simple types automatically satisfy these bounds:
20/// use dioxus_provider::prelude::*;
21///
22/// #[provider]
23/// async fn fetch_user(user_id: u32) -> Result<String, String> {
24///     // u32 implements ProviderParamBounds automatically
25///     Ok(format!("User {}", user_id))
26/// }
27///
28/// // Custom types need to derive the necessary traits:
29/// #[derive(Clone, PartialEq, Eq, Hash, Debug)]
30/// struct UserId(u32);
31///
32/// #[provider]
33/// async fn fetch_user_custom(id: UserId) -> Result<String, String> {
34///     // UserId now implements ProviderParamBounds
35///     Ok(format!("User {}", id.0))
36/// }
37/// ```
38pub trait ProviderParamBounds:
39    Clone + PartialEq + std::hash::Hash + std::fmt::Debug + Send + Sync + 'static
40{
41}
42impl<T> ProviderParamBounds for T where
43    T: Clone + PartialEq + std::hash::Hash + std::fmt::Debug + Send + Sync + 'static
44{
45}
46
47/// Common trait bounds for provider output types
48///
49/// Provider outputs must satisfy these bounds to enable:
50/// - **Clone**: Results are cloned when cached and served to multiple components
51/// - **PartialEq**: Allows change detection to avoid unnecessary re-renders
52/// - **Send + Sync**: Enables safe sharing of results across async contexts
53/// - **'static**: Required for storing results in global caches and signals
54///
55/// ## Example
56///
57/// ```rust
58/// use dioxus_provider::prelude::*;
59///
60/// // Simple types work out of the box:
61/// #[provider]
62/// async fn fetch_count() -> Result<i32, String> {
63///     Ok(42)
64/// }
65///
66/// // Custom types need to derive Clone and PartialEq:
67/// #[derive(Clone, PartialEq)]
68/// struct User {
69///     id: u32,
70///     name: String,
71/// }
72///
73/// #[provider]
74/// async fn fetch_user(id: u32) -> Result<User, String> {
75///     Ok(User { id, name: "Alice".to_string() })
76/// }
77/// ```
78pub trait ProviderOutputBounds: Clone + PartialEq + Send + Sync + 'static {}
79impl<T> ProviderOutputBounds for T where T: Clone + PartialEq + Send + Sync + 'static {}
80
81/// Common trait bounds for provider error types
82///
83/// Provider errors must satisfy these bounds to enable:
84/// - **Clone**: Errors are cloned when cached and displayed to users
85/// - **PartialEq**: Enables error comparison and change detection
86/// - **Send + Sync**: Allows safe propagation of errors across async boundaries
87/// - **'static**: Required for storing errors in caches and error states
88///
89/// ## Example
90///
91/// ```rust
92/// use dioxus_provider::prelude::*;
93/// use thiserror::Error;
94///
95/// // String works as a simple error type:
96/// #[provider]
97/// async fn simple_provider() -> Result<String, String> {
98///     Err("Something went wrong".to_string())
99/// }
100///
101/// // Custom error types provide better structure:
102/// #[derive(Error, Debug, Clone, PartialEq)]
103/// pub enum MyError {
104///     #[error("Network error: {0}")]
105///     Network(String),
106///     #[error("Not found")]
107///     NotFound,
108/// }
109///
110/// #[provider]
111/// async fn typed_provider() -> Result<String, MyError> {
112///     Err(MyError::NotFound)
113/// }
114/// ```
115pub trait ProviderErrorBounds: Clone + PartialEq + Send + Sync + 'static {}
116impl<T> ProviderErrorBounds for T where T: Clone + PartialEq + Send + Sync + 'static {}