Skip to content

Commit 7e883ea

Browse files
committed
Reduce usage of impl parameters in Rust bindings
1 parent 1bd14be commit 7e883ea

File tree

2 files changed

+64
-85
lines changed

2 files changed

+64
-85
lines changed

rust/src/ipc.rs

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,23 @@ impl Sdk {
142142
///
143143
/// # Errors
144144
/// Returns error if publish fails.
145-
pub fn publish_to_topic_json<'a>(
145+
pub fn publish_to_topic_json(
146146
&self,
147-
topic: impl Into<&'a str>,
148-
payload: impl Into<Map<'a>>,
147+
topic: &str,
148+
payload: &[Kv<'_>],
149149
) -> Result<()> {
150-
fn inner(topic: &str, payload: &Map<'_>) -> Result<()> {
151-
let topic_buf = c::GgBuffer {
152-
data: topic.as_ptr().cast_mut(),
153-
len: topic.len(),
154-
};
155-
let payload_map = c::GgMap {
156-
pairs: payload.0.as_ptr() as *mut c::GgKV,
157-
len: payload.0.len(),
158-
};
150+
let topic_buf = c::GgBuffer {
151+
data: topic.as_ptr().cast_mut(),
152+
len: topic.len(),
153+
};
154+
let payload_map = c::GgMap {
155+
pairs: payload.as_ptr() as *mut c::GgKV,
156+
len: payload.len(),
157+
};
159158

160-
Result::from(unsafe {
161-
c::ggipc_publish_to_topic_json(topic_buf, payload_map)
162-
})
163-
}
164-
inner(topic.into(), &payload.into())
159+
Result::from(unsafe {
160+
c::ggipc_publish_to_topic_json(topic_buf, payload_map)
161+
})
165162
}
166163

167164
/// Publish a binary message to a local pub/sub topic.
@@ -186,26 +183,23 @@ impl Sdk {
186183
///
187184
/// # Errors
188185
/// Returns error if publish fails.
189-
pub fn publish_to_topic_binary<'a>(
186+
pub fn publish_to_topic_binary(
190187
&self,
191-
topic: impl Into<&'a str>,
192-
payload: impl AsRef<[u8]>,
188+
topic: &str,
189+
payload: &[u8],
193190
) -> Result<()> {
194-
fn inner(topic: &str, payload: &[u8]) -> Result<()> {
195-
let topic_buf = c::GgBuffer {
196-
data: topic.as_ptr().cast_mut(),
197-
len: topic.len(),
198-
};
199-
let payload_buf = c::GgBuffer {
200-
data: payload.as_ptr().cast_mut(),
201-
len: payload.len(),
202-
};
191+
let topic_buf = c::GgBuffer {
192+
data: topic.as_ptr().cast_mut(),
193+
len: topic.len(),
194+
};
195+
let payload_buf = c::GgBuffer {
196+
data: payload.as_ptr().cast_mut(),
197+
len: payload.len(),
198+
};
203199

204-
Result::from(unsafe {
205-
c::ggipc_publish_to_topic_binary(topic_buf, payload_buf)
206-
})
207-
}
208-
inner(topic.into(), payload.as_ref())
200+
Result::from(unsafe {
201+
c::ggipc_publish_to_topic_binary(topic_buf, payload_buf)
202+
})
209203
}
210204

211205
/// Subscribe to messages on a local pub/sub topic.
@@ -219,7 +213,7 @@ impl Sdk {
219213
/// Returns error if subscription fails.
220214
pub fn subscribe_to_topic<'a, F: Fn(&str, SubscribeToTopicPayload)>(
221215
&self,
222-
topic: impl Into<&'a str>,
216+
topic: &str,
223217
callback: &'a F,
224218
) -> Result<Subscription<'a, F>> {
225219
extern "C" fn trampoline<F: Fn(&str, SubscribeToTopicPayload)>(
@@ -254,7 +248,6 @@ impl Sdk {
254248
cb(topic_str, unpacked);
255249
}
256250

257-
let topic = topic.into();
258251
let topic_buf = c::GgBuffer {
259252
data: topic.as_ptr().cast_mut(),
260253
len: topic.len(),
@@ -301,27 +294,24 @@ impl Sdk {
301294
///
302295
/// # Errors
303296
/// Returns error if publish fails.
304-
pub fn publish_to_iot_core<'a>(
297+
pub fn publish_to_iot_core(
305298
&self,
306-
topic: impl Into<&'a str>,
307-
payload: impl AsRef<[u8]>,
299+
topic: &str,
300+
payload: &[u8],
308301
qos: Qos,
309302
) -> Result<()> {
310-
fn inner(topic: &str, payload: &[u8], qos: Qos) -> Result<()> {
311-
let topic_buf = c::GgBuffer {
312-
data: topic.as_ptr().cast_mut(),
313-
len: topic.len(),
314-
};
315-
let payload_buf = c::GgBuffer {
316-
data: payload.as_ptr().cast_mut(),
317-
len: payload.len(),
318-
};
303+
let topic_buf = c::GgBuffer {
304+
data: topic.as_ptr().cast_mut(),
305+
len: topic.len(),
306+
};
307+
let payload_buf = c::GgBuffer {
308+
data: payload.as_ptr().cast_mut(),
309+
len: payload.len(),
310+
};
319311

320-
Result::from(unsafe {
321-
c::ggipc_publish_to_iot_core(topic_buf, payload_buf, qos as u8)
322-
})
323-
}
324-
inner(topic.into(), payload.as_ref(), qos)
312+
Result::from(unsafe {
313+
c::ggipc_publish_to_iot_core(topic_buf, payload_buf, qos as u8)
314+
})
325315
}
326316

327317
/// Subscribe to MQTT messages from AWS IoT Core.
@@ -335,7 +325,7 @@ impl Sdk {
335325
/// Returns error if subscription fails.
336326
pub fn subscribe_to_iot_core<'a, F: Fn(&str, &[u8])>(
337327
&self,
338-
topic_filter: impl Into<&'a str>,
328+
topic_filter: &str,
339329
qos: Qos,
340330
callback: &'a F,
341331
) -> Result<Subscription<'a, F>> {
@@ -356,7 +346,6 @@ impl Sdk {
356346
cb(topic_str, payload_bytes);
357347
}
358348

359-
let topic_filter = topic_filter.into();
360349
let topic_buf = c::GgBuffer {
361350
data: topic_filter.as_ptr().cast_mut(),
362351
len: topic_filter.len(),
@@ -561,19 +550,13 @@ impl Sdk {
561550
///
562551
/// # Errors
563552
/// Returns error if restart fails.
564-
pub fn restart_component<'a>(
565-
&self,
566-
component_name: impl Into<&'a str>,
567-
) -> Result<()> {
568-
fn inner(component_name: &str) -> Result<()> {
569-
let component_buf = c::GgBuffer {
570-
data: component_name.as_ptr().cast_mut(),
571-
len: component_name.len(),
572-
};
553+
pub fn restart_component(&self, component_name: &str) -> Result<()> {
554+
let component_buf = c::GgBuffer {
555+
data: component_name.as_ptr().cast_mut(),
556+
len: component_name.len(),
557+
};
573558

574-
Result::from(unsafe { c::ggipc_restart_component(component_buf) })
575-
}
576-
inner(component_name.into())
559+
Result::from(unsafe { c::ggipc_restart_component(component_buf) })
577560
}
578561

579562
/// Subscribe to component configuration updates.

rust/src/object.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,12 @@ impl<'a> Object<'a> {
9797
/// assert!(matches!(obj.unpack(), UnpackedObject::Buf("borrowed")));
9898
/// ```
9999
#[must_use]
100-
pub fn buf(buf: impl Into<&'a str>) -> Self {
101-
let s = buf.into();
100+
pub fn buf(buf: &'a str) -> Self {
102101
Self {
103102
c: unsafe {
104103
c::gg_obj_buf(c::GgBuffer {
105-
data: s.as_ptr().cast_mut(),
106-
len: s.len(),
104+
data: buf.as_ptr().cast_mut(),
105+
len: buf.len(),
107106
})
108107
},
109108
phantom: PhantomData,
@@ -124,13 +123,12 @@ impl<'a> Object<'a> {
124123
/// }
125124
/// ```
126125
#[must_use]
127-
pub fn list(list: impl Into<&'a [Object<'a>]>) -> Self {
128-
let slice = list.into();
126+
pub fn list(list: &'a [Object<'a>]) -> Self {
129127
Self {
130128
c: unsafe {
131129
c::gg_obj_list(c::GgList {
132-
items: slice.as_ptr().cast_mut().cast::<c::GgObject>(),
133-
len: slice.len(),
130+
items: list.as_ptr().cast_mut().cast::<c::GgObject>(),
131+
len: list.len(),
134132
})
135133
},
136134
phantom: PhantomData,
@@ -151,13 +149,12 @@ impl<'a> Object<'a> {
151149
/// }
152150
/// ```
153151
#[must_use]
154-
pub fn map(map: impl Into<&'a [Kv<'a>]>) -> Self {
155-
let slice = map.into();
152+
pub fn map(map: &'a [Kv<'a>]) -> Self {
156153
Self {
157154
c: unsafe {
158155
c::gg_obj_map(c::GgMap {
159-
pairs: slice.as_ptr().cast_mut().cast::<c::GgKV>(),
160-
len: slice.len(),
156+
pairs: map.as_ptr().cast_mut().cast::<c::GgKV>(),
157+
len: map.len(),
161158
})
162159
},
163160
phantom: PhantomData,
@@ -323,14 +320,14 @@ impl<'a> Kv<'a> {
323320
/// let kv = Kv::new("key", Object::i64(10));
324321
/// assert_eq!(kv.key(), "key");
325322
/// ```
326-
pub fn new(key: impl Into<&'a str>, value: Object<'a>) -> Self {
327-
let s = key.into();
323+
#[must_use]
324+
pub fn new(key: &'a str, value: Object<'a>) -> Self {
328325
Self {
329326
c: unsafe {
330327
c::gg_kv(
331328
c::GgBuffer {
332-
data: s.as_ptr().cast_mut(),
333-
len: s.len(),
329+
data: key.as_ptr().cast_mut(),
330+
len: key.len(),
334331
},
335332
value.c,
336333
)
@@ -410,8 +407,7 @@ impl Map<'_> {
410407
/// assert!(matches!(val.unpack(), UnpackedObject::I64(2)));
411408
/// ```
412409
#[must_use]
413-
pub fn get<'b>(&self, key: impl Into<&'b str>) -> Option<&Object<'_>> {
414-
let key = key.into();
410+
pub fn get(&self, key: &str) -> Option<&Object<'_>> {
415411
let map = c::GgMap {
416412
pairs: self.0.as_ptr().cast_mut().cast::<c::GgKV>(),
417413
len: self.0.len(),

0 commit comments

Comments
 (0)