Skip to content

Commit

Permalink
return Option
Browse files Browse the repository at this point in the history
  • Loading branch information
goatgoose committed May 24, 2024
1 parent 68c26f1 commit 60cd9af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
49 changes: 18 additions & 31 deletions bindings/rust/s2n-tls/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(&self) -> Result<Option<&T>, Error>
where
T: Send + Sync + 'static,
{
pub fn application_context<T: Send + Sync + 'static>(&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::<T>() {
None => Err(Error::INVALID_INPUT),
Some(app_context) => Ok(Some(app_context)),
},
Some(app_context) => app_context.downcast_ref::<T>(),
}
}

/// 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<T>(&mut self) -> Result<Option<&mut T>, Error>
where
T: Send + Sync + 'static,
{
pub fn application_context_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
match self.context_mut().app_context.as_mut() {
None => Ok(None),
Some(app_context) => match app_context.downcast_mut::<T>() {
None => Err(Error::INVALID_INPUT),
Some(app_context) => Ok(Some(app_context)),
},
None => None,
Some(app_context) => app_context.downcast_mut::<T>(),
}
}
}
Expand Down Expand Up @@ -1241,14 +1229,14 @@ mod tests {
let mut connection = Connection::new_server();

// Before a context is set, None is returned.
assert!(connection.application_context::<u32>().unwrap().is_none());
assert!(connection.application_context::<u32>().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::<u32>().unwrap().unwrap(),
*connection.application_context::<u32>().unwrap(),
1142
);
}
Expand All @@ -1263,12 +1251,11 @@ mod tests {

let context_value = connection
.application_context_mut::<u64>()
.unwrap()
.unwrap();
*context_value += 1;

assert_eq!(
*connection.application_context::<u64>().unwrap().unwrap(),
*connection.application_context::<u64>().unwrap(),
1
);
}
Expand All @@ -1282,7 +1269,7 @@ mod tests {
connection.set_application_context(test_value);

assert_eq!(
*connection.application_context::<u16>().unwrap().unwrap(),
*connection.application_context::<u16>().unwrap(),
1142
);

Expand All @@ -1291,7 +1278,7 @@ mod tests {
connection.set_application_context(test_value);

assert_eq!(
*connection.application_context::<u16>().unwrap().unwrap(),
*connection.application_context::<u16>().unwrap(),
10
);

Expand All @@ -1300,7 +1287,7 @@ mod tests {
connection.set_application_context(test_value);

assert_eq!(
*connection.application_context::<i16>().unwrap().unwrap(),
*connection.application_context::<i16>().unwrap(),
-20
);
}
Expand All @@ -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::<i16>().is_err());
assert!(connection.application_context::<i16>().is_none());

// Retrieving the correct type succeeds.
assert!(connection.application_context::<u32>().unwrap().is_some());
assert!(connection.application_context::<u32>().is_some());
}
}
2 changes: 0 additions & 2 deletions bindings/rust/s2n-tls/src/testing/s2n_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,6 @@ mod tests {
) -> ConnectionFutureResult {
let app_context = connection
.application_context_mut::<TestApplicationContext>()
.unwrap()
.unwrap();
app_context.invoked_count += 1;
Ok(None)
Expand Down Expand Up @@ -1028,7 +1027,6 @@ mod tests {
.0
.connection()
.application_context::<TestApplicationContext>()
.unwrap()
.unwrap();
assert_eq!(context.invoked_count, 1);
}
Expand Down

0 comments on commit 60cd9af

Please sign in to comment.