Skip to content

Commit 4522c98

Browse files
committed
allow service creation without handling the request header
1 parent 99dab88 commit 4522c98

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

examples/minimal_client_service/src/minimal_service.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::env;
33
use anyhow::{Error, Result};
44

55
fn handle_service(
6-
_request_header: &rclrs::rmw_request_id_t,
76
request: example_interfaces::srv::AddTwoInts_Request,
87
) -> example_interfaces::srv::AddTwoInts_Response {
98
println!("request: {} + {}", request.a, request.b);
@@ -17,8 +16,8 @@ fn main() -> Result<(), Error> {
1716

1817
let node = rclrs::create_node(&context, "minimal_service")?;
1918

20-
let _server = node
21-
.create_service::<example_interfaces::srv::AddTwoInts>("add_two_ints", handle_service)?;
19+
let _server =
20+
node.create_service::<example_interfaces::srv::AddTwoInts>("add_two_ints", handle_service)?;
2221

2322
println!("Starting server");
2423
rclrs::spin(node).map_err(|err| err.into())

rclrs/src/node.rs

+19
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,25 @@ impl Node {
264264
/// [1]: crate::Service
265265
// TODO: make service's lifetime depend on node's lifetime
266266
pub fn create_service<T>(
267+
&self,
268+
topic: &str,
269+
mut callback: impl FnMut(T::Request) -> T::Response + 'static + Send,
270+
) -> Result<Arc<Service<T>>, RclrsError>
271+
where
272+
T: rosidl_runtime_rs::Service,
273+
{
274+
let callback =
275+
move |_request_header: &rmw_request_id_t, request: T::Request| callback(request);
276+
self.create_service_with_header(topic, callback)
277+
}
278+
279+
/// Creates a [`Service`][1]. Same as [`create_service`][2] but the callback
280+
/// also has access to the ID of the service request.
281+
///
282+
/// [1]: crate::Service
283+
/// [2]: Self::create_service
284+
// TODO: make service's lifetime depend on node's lifetime
285+
pub fn create_service_with_header<T>(
267286
&self,
268287
topic: &str,
269288
callback: impl FnMut(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,

rclrs/src/service.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ type ServiceCallback<Request, Response> =
5151

5252
/// Main class responsible for responding to requests sent by ROS clients.
5353
///
54-
/// The only available way to instantiate services is via [`Node::create_service()`][1], this is to
55-
/// ensure that [`Node`][2]s can track all the services that have been created.
54+
/// The only available way to instantiate services is via [`Node::create_service()`][1] and
55+
/// [`Node::create_service_with_header()`][2], this is to
56+
/// ensure that [`Node`][3]s can track all the services that have been created.
5657
///
5758
/// [1]: crate::Node::create_service
58-
/// [2]: crate::Node
59+
/// [2]: crate::Node::create_service_with_header
60+
/// [3]: crate::Node
5961
pub struct Service<T>
6062
where
6163
T: rosidl_runtime_rs::Service,

0 commit comments

Comments
 (0)