From 3235c8058d6291e976722396f535eeb22971dd6a Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Wed, 17 Apr 2024 11:49:49 -0700 Subject: [PATCH] feat: deprecate set_state for insert_state --- channels/src/channel_central.rs | 2 +- compression/src/lib.rs | 2 +- head/src/lib.rs | 2 +- proxy/src/upstream/connection_counting.rs | 2 +- testing/src/test_conn.rs | 2 +- trillium/src/conn.rs | 22 +++++++++++------ trillium/src/state.rs | 2 +- .../examples/json_broadcast_websocket.rs | 2 +- websockets/examples/json_websocket.rs | 2 +- websockets/src/json.rs | 2 +- websockets/src/websocket_connection.rs | 24 ++++++++++++------- websockets/tests/json.rs | 2 +- websockets/tests/test.rs | 2 +- 13 files changed, 41 insertions(+), 27 deletions(-) diff --git a/channels/src/channel_central.rs b/channels/src/channel_central.rs index 0bb47efae2..f3f1748064 100644 --- a/channels/src/channel_central.rs +++ b/channels/src/channel_central.rs @@ -86,7 +86,7 @@ where let (client, receiver) = self.build_client(vsn); - conn.set_state(client); + conn.insert_state(client); // this is always ok because we just set the client in state self.handler.connect(ChannelConn { conn: &mut conn }).await; diff --git a/compression/src/lib.rs b/compression/src/lib.rs index 73079d5f5f..b0733404d7 100644 --- a/compression/src/lib.rs +++ b/compression/src/lib.rs @@ -172,7 +172,7 @@ impl Handler for Compression { .get_str(AcceptEncoding) .and_then(|h| self.negotiate(h)) { - conn.set_state(header); + conn.insert_state(header); } conn } diff --git a/head/src/lib.rs b/head/src/lib.rs index 8a1e1b9860..83a841b6d2 100644 --- a/head/src/lib.rs +++ b/head/src/lib.rs @@ -43,7 +43,7 @@ impl Handler for Head { async fn run(&self, mut conn: Conn) -> Conn { if conn.method() == Method::Head { conn.inner_mut().set_method(Method::Get); - conn.set_state(RequestWasHead); + conn.insert_state(RequestWasHead); } conn diff --git a/proxy/src/upstream/connection_counting.rs b/proxy/src/upstream/connection_counting.rs index 106531be62..e266827afd 100644 --- a/proxy/src/upstream/connection_counting.rs +++ b/proxy/src/upstream/connection_counting.rs @@ -59,7 +59,7 @@ where } fastrand::choice(current_selection).and_then(|(u, cc)| { - conn.set_state(ConnectionCount(cc.counter())); + conn.insert_state(ConnectionCount(cc.counter())); u.determine_upstream(conn) }) } diff --git a/testing/src/test_conn.rs b/testing/src/test_conn.rs index 90504d2a1f..6afc5f07bb 100644 --- a/testing/src/test_conn.rs +++ b/testing/src/test_conn.rs @@ -96,7 +96,7 @@ impl TestConn { where S: Send + Sync + 'static, { - self.0.set_state(state); + self.0.insert_state(state); self } diff --git a/trillium/src/conn.rs b/trillium/src/conn.rs index e22ebf9fff..ea37fb4264 100644 --- a/trillium/src/conn.rs +++ b/trillium/src/conn.rs @@ -231,7 +231,7 @@ impl Conn { struct Hello; let mut conn = get("/").on(&()); assert!(conn.state::().is_none()); - conn.set_state(Hello); + conn.insert_state(Hello); assert!(conn.state::().is_some()); ``` */ @@ -244,18 +244,26 @@ impl Conn { self.inner.state_mut().get_mut() } - /// Puts a new type into the state set. see [`Conn::state`] - /// for an example. returns the previous instance of this type, if + #[deprecated = "use Conn::insert_state"] + /// see [`insert_state`] + pub fn set_state(&mut self, state: T) -> Option { + self.insert_state(state) + } + + /// Inserts a new type into the state set. See [`Conn::state`] + /// for an example. + /// + /// Returns the previously-set instance of this type, if /// any - pub fn set_state(&mut self, val: T) -> Option { - self.inner.state_mut().insert(val) + pub fn insert_state(&mut self, state: T) -> Option { + self.inner.state_mut().insert(state) } /// Puts a new type into the state set and returns the /// `Conn`. this is useful for fluent chaining #[must_use] - pub fn with_state(mut self, val: T) -> Self { - self.set_state(val); + pub fn with_state(mut self, state: T) -> Self { + self.insert_state(state); self } diff --git a/trillium/src/state.rs b/trillium/src/state.rs index 5c933620be..b31e8e7a56 100644 --- a/trillium/src/state.rs +++ b/trillium/src/state.rs @@ -102,7 +102,7 @@ pub fn state(t: T) -> State { #[async_trait] impl Handler for State { async fn run(&self, mut conn: Conn) -> Conn { - conn.set_state(self.0.clone()); + conn.insert_state(self.0.clone()); conn } } diff --git a/websockets/examples/json_broadcast_websocket.rs b/websockets/examples/json_broadcast_websocket.rs index 2804e8e80d..f7980f8086 100644 --- a/websockets/examples/json_broadcast_websocket.rs +++ b/websockets/examples/json_broadcast_websocket.rs @@ -39,7 +39,7 @@ fn main() { trillium_logger::logger(), |mut conn: Conn| async move { if let Some(ip) = conn.peer_ip() { - conn.set_state(ip); + conn.insert_state(ip); }; conn }, diff --git a/websockets/examples/json_websocket.rs b/websockets/examples/json_websocket.rs index 7964597321..efeb511254 100644 --- a/websockets/examples/json_websocket.rs +++ b/websockets/examples/json_websocket.rs @@ -25,7 +25,7 @@ impl JsonWebSocketHandler for SomeJsonChannel { async fn connect(&self, conn: &mut WebSocketConn) -> Self::StreamType { let (s, r) = unbounded(); - conn.set_state(s); + conn.insert_state(s); r } diff --git a/websockets/src/json.rs b/websockets/src/json.rs index 7a031f337e..36427de4fd 100644 --- a/websockets/src/json.rs +++ b/websockets/src/json.rs @@ -52,7 +52,7 @@ impl JsonWebSocketHandler for SomeJsonChannel { async fn connect(&self, conn: &mut WebSocketConn) -> Self::StreamType { let (s, r) = unbounded(); - conn.set_state(s); + conn.insert_state(s); Box::pin(r) } diff --git a/websockets/src/websocket_connection.rs b/websockets/src/websocket_connection.rs index a97ad59adb..0f745a1525 100644 --- a/websockets/src/websocket_connection.rs +++ b/websockets/src/websocket_connection.rs @@ -141,10 +141,10 @@ impl WebSocketConn { an empty string if there is no query component. */ pub fn querystring(&self) -> &str { - match self.path.split_once('?') { - Some((_, query)) => query, - None => "", - } + self.path + .split_once('?') + .map(|(_, q)| q) + .unwrap_or_default() } /// retrieve the request method for this conn @@ -169,11 +169,17 @@ impl WebSocketConn { self.state.get_mut() } - /** - set state on this connection - */ - pub fn set_state(&mut self, val: T) { - self.state.insert(val); + /// see [`insert_state`] + #[deprecated = "use WebsocketConn::insert_state"] + pub fn set_state(&mut self, state: T) { + self.insert_state(state); + } + + /// inserts new state + /// + /// returns the previously set state of the same type, if any existed + pub fn insert_state(&mut self, state: T) -> Option { + self.state.insert(state) } /** diff --git a/websockets/tests/json.rs b/websockets/tests/json.rs index 573883a858..954e889a0f 100644 --- a/websockets/tests/json.rs +++ b/websockets/tests/json.rs @@ -34,7 +34,7 @@ impl JsonWebSocketHandler for SomeJsonChannel { async fn connect(&self, conn: &mut WebSocketConn) -> Self::StreamType { let (s, r) = unbounded(); - conn.set_state(s); + conn.insert_state(s); Box::pin(r) } diff --git a/websockets/tests/test.rs b/websockets/tests/test.rs index f4d252c968..9d89b3545a 100644 --- a/websockets/tests/test.rs +++ b/websockets/tests/test.rs @@ -34,7 +34,7 @@ fn with_channel() { mut conn: WebSocketConn, ) -> Option<(WebSocketConn, Self::OutboundStream)> { let (send, receive) = async_channel::unbounded(); - conn.set_state(send); + conn.insert_state(send); Some((conn, Box::pin(receive))) }