Skip to content

Commit

Permalink
Merge pull request #142 from entire/feat/entire-pass-in-closures
Browse files Browse the repository at this point in the history
Be able to pass in closure to advertise_service
  • Loading branch information
Carter12s authored Oct 24, 2023
2 parents fe2827e + 113df2c commit c031195
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- The build.rs example in example_package now correctly informs cargo of filesystem dependencies
- The `advertise_serveice` method in `rosbridge/client.rs` now accepts closures

### Fixed

Expand Down
14 changes: 13 additions & 1 deletion roslibrust/examples/service_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ roslibrust_codegen_macro::find_and_generate_ros_messages!("assets/ros1_common_in
// a canned response.
fn my_service(
request: std_srvs::SetBoolRequest,
my_string: &str,
) -> Result<std_srvs::SetBoolResponse, Box<dyn std::error::Error + Send + Sync>> {
log::info!("Got request to set bool: {request:?}");
log::info!("Using my string: {}", my_string); // Use the string here

Ok(std_srvs::SetBoolResponse {
success: true,
message: "You set my bool!".to_string(),
Expand Down Expand Up @@ -48,10 +51,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Create a new client
let client = ClientHandle::new("ws://localhost:9090").await?;

// The string you want to pass in to the closure
let my_string = "Some string";

// Actually advertise our service
// The handle returned here establishes the lifetime of our service and dropping it will unadvertise the service
let _handle = client
.advertise_service::<std_srvs::SetBool>("/my_set_bool", my_service)
.advertise_service::<std_srvs::SetBool, _>(
"/my_set_bool",
move |request: std_srvs::SetBoolRequest| -> Result<
std_srvs::SetBoolResponse,
Box<dyn std::error::Error + Send + Sync>,
> { my_service(request, my_string) },
)
.await?;

// Now try manually calling the service with the command line!
Expand Down
19 changes: 13 additions & 6 deletions roslibrust/src/rosbridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,21 @@ impl ClientHandle {
/// Service will be active until the handle is dropped!
///
/// See examples/service_server.rs for usage.
pub async fn advertise_service<T: RosServiceType>(
pub async fn advertise_service<T, F>(
&self,
topic: &str,
server: fn(
T::Request,
)
-> Result<T::Response, Box<dyn std::error::Error + 'static + Send + Sync>>,
) -> RosLibRustResult<ServiceHandle> {
server: F,
) -> RosLibRustResult<ServiceHandle>
where
T: RosServiceType,
F: Fn(
T::Request,
)
-> Result<T::Response, Box<dyn std::error::Error + 'static + Send + Sync>>
+ Send
+ Sync
+ 'static,
{
self.check_for_disconnect()?;
{
let client = self.inner.read().await;
Expand Down
2 changes: 1 addition & 1 deletion roslibrust/src/rosbridge/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod integration_tests {
let topic = "/self_service_call";

let handle = client
.advertise_service::<SetBool>(topic, cb)
.advertise_service::<SetBool, _>(topic, cb)
.await
.expect("Failed to advertise service");

Expand Down

0 comments on commit c031195

Please sign in to comment.