Skip to content

Commit 62dbc6b

Browse files
committed
Make Rust IPC APIs more flexible
1 parent 6d0bc4c commit 62dbc6b

File tree

2 files changed

+77
-55
lines changed

2 files changed

+77
-55
lines changed

rust/src/ipc.rs

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,24 @@ impl Sdk {
125125
/// Returns error if publish fails.
126126
pub fn publish_to_topic_json<'a>(
127127
&self,
128-
topic: &str,
128+
topic: impl Into<&'a str>,
129129
payload: impl Into<MapRef<'a>>,
130130
) -> Result<()> {
131-
let payload = payload.into();
132-
let topic_buf = c::GglBuffer {
133-
data: topic.as_ptr().cast_mut(),
134-
len: topic.len(),
135-
};
136-
let payload_map = c::GglMap {
137-
pairs: payload.0.as_ptr() as *mut c::GglKV,
138-
len: payload.0.len(),
139-
};
131+
fn inner(topic: &str, payload: &MapRef<'_>) -> Result<()> {
132+
let topic_buf = c::GglBuffer {
133+
data: topic.as_ptr().cast_mut(),
134+
len: topic.len(),
135+
};
136+
let payload_map = c::GglMap {
137+
pairs: payload.0.as_ptr() as *mut c::GglKV,
138+
len: payload.0.len(),
139+
};
140140

141-
Result::from(unsafe {
142-
c::ggipc_publish_to_topic_json(topic_buf, payload_map)
143-
})
141+
Result::from(unsafe {
142+
c::ggipc_publish_to_topic_json(topic_buf, payload_map)
143+
})
144+
}
145+
inner(topic.into(), &payload.into())
144146
}
145147

146148
/// Publish a binary message to a local pub/sub topic.
@@ -165,23 +167,26 @@ impl Sdk {
165167
///
166168
/// # Errors
167169
/// Returns error if publish fails.
168-
pub fn publish_to_topic_binary(
170+
pub fn publish_to_topic_binary<'a>(
169171
&self,
170-
topic: &str,
171-
payload: &[u8],
172+
topic: impl Into<&'a str>,
173+
payload: impl AsRef<[u8]>,
172174
) -> Result<()> {
173-
let topic_buf = c::GglBuffer {
174-
data: topic.as_ptr().cast_mut(),
175-
len: topic.len(),
176-
};
177-
let payload_buf = c::GglBuffer {
178-
data: payload.as_ptr().cast_mut(),
179-
len: payload.len(),
180-
};
175+
fn inner(topic: &str, payload: &[u8]) -> Result<()> {
176+
let topic_buf = c::GglBuffer {
177+
data: topic.as_ptr().cast_mut(),
178+
len: topic.len(),
179+
};
180+
let payload_buf = c::GglBuffer {
181+
data: payload.as_ptr().cast_mut(),
182+
len: payload.len(),
183+
};
181184

182-
Result::from(unsafe {
183-
c::ggipc_publish_to_topic_binary(topic_buf, payload_buf)
184-
})
185+
Result::from(unsafe {
186+
c::ggipc_publish_to_topic_binary(topic_buf, payload_buf)
187+
})
188+
}
189+
inner(topic.into(), payload.as_ref())
185190
}
186191

187192
/// Subscribe to messages on a local pub/sub topic.
@@ -198,7 +203,7 @@ impl Sdk {
198203
F: FnMut(&str, SubscribeToTopicPayload) + 'a,
199204
>(
200205
&self,
201-
topic: &str,
206+
topic: impl Into<&'a str>,
202207
callback: F,
203208
) -> Result<Subscription<'a, F>> {
204209
extern "C" fn trampoline<F: FnMut(&str, SubscribeToTopicPayload)>(
@@ -236,6 +241,7 @@ impl Sdk {
236241
cb(topic_str, unpacked);
237242
}
238243

244+
let topic = topic.into();
239245
let topic_buf = c::GglBuffer {
240246
data: topic.as_ptr().cast_mut(),
241247
len: topic.len(),
@@ -286,24 +292,27 @@ impl Sdk {
286292
///
287293
/// # Errors
288294
/// Returns error if publish fails.
289-
pub fn publish_to_iot_core(
295+
pub fn publish_to_iot_core<'a>(
290296
&self,
291-
topic: &str,
292-
payload: &[u8],
297+
topic: impl Into<&'a str>,
298+
payload: impl AsRef<[u8]>,
293299
qos: Qos,
294300
) -> Result<()> {
295-
let topic_buf = c::GglBuffer {
296-
data: topic.as_ptr().cast_mut(),
297-
len: topic.len(),
298-
};
299-
let payload_buf = c::GglBuffer {
300-
data: payload.as_ptr().cast_mut(),
301-
len: payload.len(),
302-
};
301+
fn inner(topic: &str, payload: &[u8], qos: Qos) -> Result<()> {
302+
let topic_buf = c::GglBuffer {
303+
data: topic.as_ptr().cast_mut(),
304+
len: topic.len(),
305+
};
306+
let payload_buf = c::GglBuffer {
307+
data: payload.as_ptr().cast_mut(),
308+
len: payload.len(),
309+
};
303310

304-
Result::from(unsafe {
305-
c::ggipc_publish_to_iot_core(topic_buf, payload_buf, qos as u8)
306-
})
311+
Result::from(unsafe {
312+
c::ggipc_publish_to_iot_core(topic_buf, payload_buf, qos as u8)
313+
})
314+
}
315+
inner(topic.into(), payload.as_ref(), qos)
307316
}
308317

309318
/// Subscribe to MQTT messages from AWS IoT Core.
@@ -317,7 +326,7 @@ impl Sdk {
317326
/// Returns error if subscription fails.
318327
pub fn subscribe_to_iot_core<'a, F: FnMut(&str, &[u8]) + 'a>(
319328
&self,
320-
topic_filter: &str,
329+
topic_filter: impl Into<&'a str>,
321330
qos: Qos,
322331
callback: F,
323332
) -> Result<Subscription<'a, F>> {
@@ -337,6 +346,7 @@ impl Sdk {
337346
cb(topic_str, payload_bytes);
338347
}
339348

349+
let topic_filter = topic_filter.into();
340350
let topic_buf = c::GglBuffer {
341351
data: topic_filter.as_ptr().cast_mut(),
342352
len: topic_filter.len(),
@@ -485,24 +495,24 @@ impl Sdk {
485495
/// # Examples
486496
///
487497
/// ```no_run
488-
/// use ggl_sdk::{Sdk, object::Object};
498+
/// use ggl_sdk::Sdk;
489499
///
490500
/// let sdk = Sdk::init();
491501
/// sdk.connect()?;
492502
///
493-
/// let new_value = Object::i64(100);
494-
/// sdk.update_config(&["maxRetries"], None, &new_value.as_ref())?;
503+
/// sdk.update_config(&["maxRetries"], None, 100_i64)?;
495504
/// # Ok::<(), ggl_sdk::Error>(())
496505
/// ```
497506
///
498507
/// # Errors
499508
/// Returns error if config update fails.
500-
pub fn update_config(
509+
pub fn update_config<'a>(
501510
&self,
502511
key_path: &[&str],
503512
timestamp: Option<std::time::SystemTime>,
504-
value_to_merge: &ObjectRef<'_>,
513+
value_to_merge: impl Into<ObjectRef<'a>>,
505514
) -> Result<()> {
515+
let value_to_merge = value_to_merge.into();
506516
let bufs: Box<[c::GglBuffer]> = key_path
507517
.iter()
508518
.map(|k| c::GglBuffer {
@@ -530,7 +540,7 @@ impl Sdk {
530540
c::ggipc_update_config(
531541
key_path_list,
532542
timespec.as_ref().map_or(ptr::null(), ptr::from_ref),
533-
*ptr::from_ref(value_to_merge).cast::<c::GglObject>(),
543+
*ptr::from_ref(&value_to_merge).cast::<c::GglObject>(),
534544
)
535545
})
536546
}
@@ -556,13 +566,19 @@ impl Sdk {
556566
///
557567
/// # Errors
558568
/// Returns error if restart fails.
559-
pub fn restart_component(&self, component_name: &str) -> Result<()> {
560-
let component_buf = c::GglBuffer {
561-
data: component_name.as_ptr().cast_mut(),
562-
len: component_name.len(),
563-
};
569+
pub fn restart_component<'a>(
570+
&self,
571+
component_name: impl Into<&'a str>,
572+
) -> Result<()> {
573+
fn inner(component_name: &str) -> Result<()> {
574+
let component_buf = c::GglBuffer {
575+
data: component_name.as_ptr().cast_mut(),
576+
len: component_name.len(),
577+
};
564578

565-
Result::from(unsafe { c::ggipc_restart_component(component_buf) })
579+
Result::from(unsafe { c::ggipc_restart_component(component_buf) })
580+
}
581+
inner(component_name.into())
566582
}
567583

568584
/// Subscribe to component configuration updates.

rust/src/object.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ impl<'a> From<&'a [KvRef<'a>]> for ObjectRef<'a> {
550550
}
551551
}
552552

553+
impl<'a> From<&'a ObjectRef<'a>> for ObjectRef<'a> {
554+
fn from(obj: &'a ObjectRef<'a>) -> Self {
555+
unsafe { ptr::read(obj) }
556+
}
557+
}
558+
553559
impl<'a> From<MapRef<'a>> for ObjectRef<'a> {
554560
fn from(map: MapRef<'a>) -> Self {
555561
Self::map(map.0)

0 commit comments

Comments
 (0)