Skip to content

Commit

Permalink
Integrate RMW message methods into ActionImpl
Browse files Browse the repository at this point in the history
Rather than having a bunch of standalone traits implementing various
message functions like `ExtractUuid` and `SetAccepted`, with the
trait bounds on each associated type in `ActionImpl`, we'll instead add
these functions directly to the `ActionImpl` trait. This is simpler on
both the rosidl_runtime_rs and the rclrs side.
  • Loading branch information
nwn committed Aug 7, 2024
1 parent ecc2909 commit eee3464
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
12 changes: 5 additions & 7 deletions rclrs/src/action/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
wait::WaitableNumEntities,
Clock, DropGuard, NodeHandle, RclrsError, ENTITY_LIFECYCLE_MUTEX,
};
use rosidl_runtime_rs::{Action, Message, Service};
use rosidl_runtime_rs::{Action, ActionImpl, Message, Service};
use std::{
ffi::CString,
sync::{atomic::AtomicBool, Arc, Mutex, MutexGuard},
Expand Down Expand Up @@ -165,7 +165,7 @@ where
writer_guid: [0; 16],
sequence_number: 0,
};
type RmwRequest<T> = <<<T as rosidl_runtime_rs::ActionImpl>::SendGoalService as Service>::Request as Message>::RmwMsg;
type RmwRequest<T> = <<<T as ActionImpl>::SendGoalService as Service>::Request as Message>::RmwMsg;
let mut request_rmw = RmwRequest::<T>::default();
let handle = &*self.handle.lock();
unsafe {
Expand All @@ -186,10 +186,9 @@ where
mut request_id: rmw_request_id_t,
accepted: bool,
) -> Result<(), RclrsError> {
type RmwResponse<T> = <<<T as rosidl_runtime_rs::ActionImpl>::SendGoalService as Service>::Response as Message>::RmwMsg;
type RmwResponse<T> = <<<T as ActionImpl>::SendGoalService as Service>::Response as Message>::RmwMsg;
let mut response_rmw = RmwResponse::<T>::default();
// TODO(nwn): Set the `accepted` field through a trait, similarly to how we extracted the UUID.
// response_rmw.accepted = accepted;
<T as ActionImpl>::set_goal_response_accepted(&mut response_rmw, accepted);
let handle = &*self.handle.lock();
let result = unsafe {
// SAFETY: The action server handle is locked and so synchronized with other
Expand Down Expand Up @@ -232,8 +231,7 @@ where
Err(err) => return Err(err),
};

let mut uuid = GoalUuid::default();
rosidl_runtime_rs::ExtractUuid::extract_uuid(&request, &mut uuid.0);
let uuid = GoalUuid(<T as ActionImpl>::get_goal_request_uuid(&request));

let response: GoalResponse = {
todo!("Optionally convert request to an idiomatic type for the user's callback.");
Expand Down
2 changes: 1 addition & 1 deletion rosidl_runtime_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ mod string;
pub use string::{BoundedString, BoundedWString, String, StringExceedsBoundsError, WString};

mod traits;
pub use traits::{Action, ActionImpl, ExtractUuid, Message, RmwMessage, SequenceAlloc, Service};
pub use traits::{Action, ActionImpl, Message, RmwMessage, SequenceAlloc, Service};
14 changes: 6 additions & 8 deletions rosidl_runtime_rs/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,17 @@ pub trait ActionImpl: 'static {
type FeedbackMessage: Message;

/// The send_goal service associated with this action.
type SendGoalService: Service<Request: Message<RmwMsg: ExtractUuid>>;
type SendGoalService: Service;

/// The cancel_goal service associated with this action.
type CancelGoalService: Service;

/// The get_result service associated with this action.
type GetResultService: Service;
}

/// Trait for types containing a special UUID field.
///
/// User code never needs to implement this trait, nor call its method.
pub trait ExtractUuid: 'static {
/// Copies the UUID field from `self` into the provided buffer.
fn extract_uuid(&self, bytes: &mut [u8; 16]);
/// Get the UUID of a goal request.
fn get_goal_request_uuid(request: &<<Self::SendGoalService as Service>::Request as Message>::RmwMsg) -> [u8; 16];

/// Sets the `accepted` field of a goal response.
fn set_goal_response_accepted(response: &mut <<Self::SendGoalService as Service>::Response as Message>::RmwMsg, accepted: bool);
}

0 comments on commit eee3464

Please sign in to comment.