1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use ;
/// An extractor for server-wide shared state, modeled after axum's
/// `State<T>`.
///
/// Register the value once via `LynnServer::with_state(value)`, then declare
/// it as a handler parameter — the framework injects an `Arc<T>` snapshot of
/// the shared value into every request:
///
/// ```rust,no_run
/// use lynn_tcp::{lynn_server::*, lynn_tcp_dependents::*, lynn_state::AppState};
///
/// struct UserRepo { /* e.g. a sea_orm::DatabaseConnection */ }
/// impl UserRepo {
/// fn find_user(&self, id: u64) -> String { format!("user-{id}") }
/// }
///
/// #[tokio::main]
/// async fn main() {
/// LynnServer::new()
/// .await
/// .with_state(UserRepo {})
/// .add_router(1, find_user_handler)
/// .start()
/// .await;
/// }
///
/// async fn find_user_handler(repo: AppState<UserRepo>, input: InputBufVO) -> HandlerResult {
/// // `repo` dereferences to `&UserRepo`.
/// let name = repo.find_user(1);
/// let addr = input.get_input_addr().unwrap();
/// HandlerResult::new_with_send(1, name.into(), vec![addr])
/// }
/// ```
///
/// Multiple state types can coexist: register each type once with
/// `with_state`, and handlers may take several `AppState<T>` parameters.
/// `AppState<T>` dereferences to `T`, so state methods can be called
/// directly. Extraction panics with a descriptive message when the value was
/// never registered — configure states before `start()`.