diff --git a/bindings/rust/s2n-tls/src/connection.rs b/bindings/rust/s2n-tls/src/connection.rs index 13443db232d..94eb683d6fc 100644 --- a/bindings/rust/s2n-tls/src/connection.rs +++ b/bindings/rust/s2n-tls/src/connection.rs @@ -1060,44 +1060,32 @@ impl Connection { /// Retrieves a reference to the application context associated with the Connection. /// - /// If an application context hasn't already been set on the Connection, None will be returned. - /// An error will be returned if the set application context isn't of the specified type. + /// If an application context hasn't already been set on the Connection, or if the set + /// application context isn't of type T, None will be returned. /// /// To set a context on the connection, use [`Self::set_application_context()`]. To retrieve a /// mutable reference to the context, use [`Self::application_context_mut()`]. - pub fn application_context(&self) -> Result, Error> - where - T: Send + Sync + 'static, - { + pub fn application_context(&self) -> Option<&T> { match self.context().app_context.as_ref() { - None => Ok(None), + None => None, // The Any trait keeps track of the application context's type. downcast_ref() returns // Some only if the correct type is provided: // https://doc.rust-lang.org/std/any/trait.Any.html#method.downcast_ref - Some(app_context) => match app_context.downcast_ref::() { - None => Err(Error::INVALID_INPUT), - Some(app_context) => Ok(Some(app_context)), - }, + Some(app_context) => app_context.downcast_ref::(), } } /// Retrieves a mutable reference to the application context associated with the Connection. /// - /// If an application context hasn't already been set on the Connection, None will be returned. - /// An error will be returned if the set application context isn't of the specified type. + /// If an application context hasn't already been set on the Connection, or if the set + /// application context isn't of type T, None will be returned. /// /// To set a context on the connection, use [`Self::set_application_context()`]. To retrieve an /// immutable reference to the context, use [`Self::application_context()`]. - pub fn application_context_mut(&mut self) -> Result, Error> - where - T: Send + Sync + 'static, - { + pub fn application_context_mut(&mut self) -> Option<&mut T> { match self.context_mut().app_context.as_mut() { - None => Ok(None), - Some(app_context) => match app_context.downcast_mut::() { - None => Err(Error::INVALID_INPUT), - Some(app_context) => Ok(Some(app_context)), - }, + None => None, + Some(app_context) => app_context.downcast_mut::(), } } } @@ -1241,14 +1229,14 @@ mod tests { let mut connection = Connection::new_server(); // Before a context is set, None is returned. - assert!(connection.application_context::().unwrap().is_none()); + assert!(connection.application_context::().is_none()); let test_value: u32 = 1142; connection.set_application_context(test_value); // After a context is set, the application data is returned. assert_eq!( - *connection.application_context::().unwrap().unwrap(), + *connection.application_context::().unwrap(), 1142 ); } @@ -1263,12 +1251,11 @@ mod tests { let context_value = connection .application_context_mut::() - .unwrap() .unwrap(); *context_value += 1; assert_eq!( - *connection.application_context::().unwrap().unwrap(), + *connection.application_context::().unwrap(), 1 ); } @@ -1282,7 +1269,7 @@ mod tests { connection.set_application_context(test_value); assert_eq!( - *connection.application_context::().unwrap().unwrap(), + *connection.application_context::().unwrap(), 1142 ); @@ -1291,7 +1278,7 @@ mod tests { connection.set_application_context(test_value); assert_eq!( - *connection.application_context::().unwrap().unwrap(), + *connection.application_context::().unwrap(), 10 ); @@ -1300,7 +1287,7 @@ mod tests { connection.set_application_context(test_value); assert_eq!( - *connection.application_context::().unwrap().unwrap(), + *connection.application_context::().unwrap(), -20 ); } @@ -1314,9 +1301,9 @@ mod tests { connection.set_application_context(test_value); // A context type that wasn't set shouldn't be returned. - assert!(connection.application_context::().is_err()); + assert!(connection.application_context::().is_none()); // Retrieving the correct type succeeds. - assert!(connection.application_context::().unwrap().is_some()); + assert!(connection.application_context::().is_some()); } } diff --git a/bindings/rust/s2n-tls/src/testing/s2n_tls.rs b/bindings/rust/s2n-tls/src/testing/s2n_tls.rs index 27b30208bc8..eb18e7d16bd 100644 --- a/bindings/rust/s2n-tls/src/testing/s2n_tls.rs +++ b/bindings/rust/s2n-tls/src/testing/s2n_tls.rs @@ -987,7 +987,6 @@ mod tests { ) -> ConnectionFutureResult { let app_context = connection .application_context_mut::() - .unwrap() .unwrap(); app_context.invoked_count += 1; Ok(None) @@ -1028,7 +1027,6 @@ mod tests { .0 .connection() .application_context::() - .unwrap() .unwrap(); assert_eq!(context.invoked_count, 1); }