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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Connection state types for type-state pattern.
//!
//! The type-state pattern ensures at compile time that certain operations
//! can only be performed when the connection is in the appropriate state.
//!
//! ## State Transitions
//!
//! ```text
//! Disconnected -> Connected (via TCP connect)
//! Connected -> Ready (via authentication)
//! Ready -> InTransaction (via begin_transaction())
//! InTransaction -> Ready (via commit() or rollback())
//! ```
//!
//! Queries do not change the connection state: `query()` borrows the client
//! mutably and returns an iterable result.
//!
//! Incremental streaming (`query_stream` / `query_stream_blob`) likewise does
//! **not** transition into a dedicated state. Rather than the [`Streaming`]
//! type-state originally sketched, the streams borrow the client mutably
//! (`&mut Client<S>`) for their lifetime: the borrow checker already enforces
//! that no other request can run on the connection until the stream is dropped,
//! and a `&mut` borrow — unlike a consuming state transition — works uniformly
//! for both [`Ready`] and [`InTransaction`] clients and returns the borrow
//! automatically. The [`Streaming`] marker is therefore retained but unused.
use PhantomData;
/// Marker trait for connection states.
///
/// This trait is sealed to prevent external implementations,
/// ensuring that only the states defined in this crate are valid.
/// Connection is not yet established.
///
/// In this state, only `connect()` can be called.
;
/// TCP connection established, awaiting authentication.
///
/// In this intermediate state:
/// - TCP connection is open
/// - TLS negotiation may be in progress or complete
/// - Login/authentication has not yet completed
///
/// This state is mostly internal; users typically go directly from
/// `Disconnected` to `Ready` via `Client::connect()`.
;
/// Connection is established and ready for queries.
///
/// In this state, queries can be executed and transactions can be started.
;
/// Connection is in a transaction.
///
/// In this state, queries execute within the transaction context.
/// The transaction must be explicitly committed or rolled back.
;
/// Connection is actively streaming results.
///
/// In this state, the connection is processing a result set.
/// No other operations can be performed until the stream is
/// consumed or cancelled.
;
/// Type-level state transition marker.
///
/// This is used internally to track state transitions at compile time.
/// Internal protocol state for runtime management.
///
/// While connection states are tracked at compile-time via type-state,
/// the protocol layer has runtime state that must be managed.