diff --git a/src/subsystem/subsystem_handle.rs b/src/subsystem/subsystem_handle.rs index 1547029..5539417 100644 --- a/src/subsystem/subsystem_handle.rs +++ b/src/subsystem/subsystem_handle.rs @@ -328,6 +328,16 @@ impl SubsystemHandle { pub fn create_cancellation_token(&self) -> CancellationToken { self.inner.cancellation_token.child_token() } + + /// Get the name associated with this subsystem. + /// + /// Note that the names of nested subsystems are built unix-path alike, + /// starting and delimited by slashes (e.g. `/a/b/c`). + /// + /// See [`SubsystemBuilder::new()`] how to set this name. + pub fn name(&self) -> &str { + &self.inner.name + } } impl Drop for SubsystemHandle { diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 463d463..3b8b7b2 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -920,3 +920,25 @@ async fn shutdown_through_signal() { }, ); } + +#[tokio::test] +#[traced_test] +async fn access_name_from_within_subsystem() { + let subsys_nested = move |subsys: SubsystemHandle| async move { + assert_eq!("/subsys_top/subsys_nested", subsys.name()); + BoxedResult::Ok(()) + }; + + let subsys_top = move |subsys: SubsystemHandle| async move { + assert_eq!("/subsys_top", subsys.name()); + subsys.start(SubsystemBuilder::new("subsys_nested", subsys_nested)); + BoxedResult::Ok(()) + }; + + Toplevel::new(move |s| async move { + s.start(SubsystemBuilder::new("subsys_top", subsys_top)); + }) + .handle_shutdown_requests(Duration::from_millis(100)) + .await + .unwrap(); +}