Skip to content

Commit da05361

Browse files
committed
Enable direct creation of the _Options for all primitive types
Signed-off-by: Michael X. Grey <[email protected]>
1 parent 6c61c9c commit da05361

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

rclrs/src/client.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,25 @@ pub struct ClientOptions<'a> {
294294
pub qos: QoSProfile,
295295
}
296296

297-
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ClientOptions<'a> {
298-
fn from(value: T) -> Self {
299-
let options = value.into_primitive_options();
300-
let mut qos = QoSProfile::services_default();
301-
options.apply(&mut qos);
297+
impl<'a> ClientOptions<'a> {
298+
/// Initialize a new [`ClientOptions`] with default settings.
299+
pub fn new(service_name: &'a str) -> Self {
302300
Self {
303-
service_name: options.name,
304-
qos,
301+
service_name,
302+
qos: QoSProfile::services_default(),
305303
}
306304
}
307305
}
308306

307+
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ClientOptions<'a> {
308+
fn from(value: T) -> Self {
309+
let primitive = value.into_primitive_options();
310+
let mut options = Self::new(primitive.name);
311+
primitive.apply(&mut options.qos);
312+
options
313+
}
314+
}
315+
309316
impl<T> ClientBase for Client<T>
310317
where
311318
T: rosidl_runtime_rs::Service,

rclrs/src/publisher.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,25 @@ pub struct PublisherOptions<'a> {
244244
pub qos: QoSProfile,
245245
}
246246

247-
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for PublisherOptions<'a> {
248-
fn from(value: T) -> Self {
249-
let options = value.into_primitive_options();
250-
let mut qos = QoSProfile::topics_default();
251-
options.apply(&mut qos);
247+
impl<'a> PublisherOptions<'a> {
248+
/// Initialize a new [`PublisherOptions`] with default settings.
249+
pub fn new(topic: &'a str) -> Self {
252250
Self {
253-
topic: options.name,
254-
qos,
251+
topic,
252+
qos: QoSProfile::topics_default(),
255253
}
256254
}
257255
}
258256

257+
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for PublisherOptions<'a> {
258+
fn from(value: T) -> Self {
259+
let primitive = value.into_primitive_options();
260+
let mut options = Self::new(primitive.name);
261+
primitive.apply(&mut options.qos);
262+
options
263+
}
264+
}
265+
259266
/// Convenience trait for [`Publisher::publish`].
260267
pub trait MessageCow<'a, T: Message> {
261268
/// Wrap the owned or borrowed message in a `Cow`.

rclrs/src/service.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,29 @@ where
190190
pub struct ServiceOptions<'a> {
191191
/// The name for the service
192192
pub name: &'a str,
193-
/// The quality of service for the service.
193+
/// The quality of service profile for the service.
194194
pub qos: QoSProfile,
195195
}
196196

197-
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ServiceOptions<'a> {
198-
fn from(value: T) -> Self {
199-
let options = value.into_primitive_options();
200-
let mut qos = QoSProfile::services_default();
201-
options.apply(&mut qos);
197+
impl<'a> ServiceOptions<'a> {
198+
/// Initialize a new [`ServiceOptions`] with default settings.
199+
pub fn new(name: &'a str) -> Self {
202200
Self {
203-
name: options.name,
204-
qos,
201+
name,
202+
qos: QoSProfile::services_default(),
205203
}
206204
}
207205
}
208206

207+
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for ServiceOptions<'a> {
208+
fn from(value: T) -> Self {
209+
let primitive = value.into_primitive_options();
210+
let mut options = Self::new(primitive.name);
211+
primitive.apply(&mut options.qos);
212+
options
213+
}
214+
}
215+
209216
impl<T> ServiceBase for Service<T>
210217
where
211218
T: rosidl_runtime_rs::Service,

rclrs/src/subscription.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,25 @@ pub struct SubscriptionOptions<'a> {
274274
pub qos: QoSProfile,
275275
}
276276

277-
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for SubscriptionOptions<'a> {
278-
fn from(value: T) -> Self {
279-
let options = value.into_primitive_options();
280-
// Topics will use the QOS_PROFILE_DEFAULT by default, which is designed
281-
// to roughly match the ROS 1 default topic behavior.
282-
let mut qos = QoSProfile::topics_default();
283-
options.apply(&mut qos);
277+
impl<'a> SubscriptionOptions<'a> {
278+
/// Initialize a new [`SubscriptionOptions`] with default settings.
279+
pub fn new(topic: &'a str) -> Self {
284280
Self {
285-
topic: options.name,
286-
qos,
281+
topic,
282+
qos: QoSProfile::topics_default(),
287283
}
288284
}
289285
}
290286

287+
impl<'a, T: IntoPrimitiveOptions<'a>> From<T> for SubscriptionOptions<'a> {
288+
fn from(value: T) -> Self {
289+
let primitive = value.into_primitive_options();
290+
let mut options = Self::new(primitive.name);
291+
primitive.apply(&mut options.qos);
292+
options
293+
}
294+
}
295+
291296
impl<T> SubscriptionBase for Subscription<T>
292297
where
293298
T: Message,

0 commit comments

Comments
 (0)