Skip to content

Commit b3cf3b0

Browse files
committed
feat: add spring_pop_non_blocking
1 parent e6bc4a8 commit b3cf3b0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

springql.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ enum SpringErrno spring_command(const SpringPipeline *pipeline, const char *sql)
129129
*/
130130
SpringRow *spring_pop(const SpringPipeline *pipeline, const char *queue);
131131

132+
/**
133+
* See: springql_core::api::spring_pop_non_blocking
134+
*
135+
* # Returns
136+
*
137+
* - non-NULL: Successfully get a row.
138+
* - NULL: Error occurred if `is_err` is true (check spring_last_err() for details). Otherwise, any row is not in the queue.
139+
*
140+
* # Safety
141+
*
142+
* This function is unsafe because it uses raw pointer.
143+
*/
144+
SpringRow *spring_pop_non_blocking(const SpringPipeline *pipeline,
145+
const char *queue,
146+
bool *is_err);
147+
132148
/**
133149
* # Returns
134150
*

src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,42 @@ pub unsafe extern "C" fn spring_pop(
174174
)
175175
}
176176

177+
/// See: springql_core::api::spring_pop_non_blocking
178+
///
179+
/// # Returns
180+
///
181+
/// - non-NULL: Successfully get a row.
182+
/// - NULL: Error occurred if `is_err` is true (check spring_last_err() for details). Otherwise, any row is not in the queue.
183+
///
184+
/// # Safety
185+
///
186+
/// This function is unsafe because it uses raw pointer.
187+
#[no_mangle]
188+
pub unsafe extern "C" fn spring_pop_non_blocking(
189+
pipeline: *const SpringPipeline,
190+
queue: *const c_char,
191+
is_err: *mut bool,
192+
) -> *mut SpringRow {
193+
*is_err = false;
194+
195+
let pipeline = &*((*pipeline).0 as *const springql_core::SpringPipeline);
196+
let queue = CStr::from_ptr(queue).to_string_lossy().into_owned();
197+
198+
with_catch(|| springql_core::spring_pop_non_blocking(pipeline, &queue)).map_or_else(
199+
|_| {
200+
*is_err = true;
201+
ptr::null_mut()
202+
},
203+
|opt_row| {
204+
if let Some(row) = opt_row {
205+
Box::into_raw(Box::new(SpringRow(mem::transmute(Box::new(row)))))
206+
} else {
207+
ptr::null_mut()
208+
}
209+
},
210+
)
211+
}
212+
177213
/// # Returns
178214
///
179215
/// - `0`: if there are no recent errors.

0 commit comments

Comments
 (0)