use super::*;
use crate::Params;
pub struct QueryResponseList {
inner: NonNull<bindings::nixl_capi_query_resp_list_s>,
}
pub struct QueryResponse<'a> {
list: &'a QueryResponseList,
index: usize,
}
impl QueryResponseList {
pub fn new() -> Result<Self, NixlError> {
let mut list = ptr::null_mut();
let status = unsafe { nixl_capi_create_query_resp_list(&mut list) };
match status {
NIXL_CAPI_SUCCESS => {
let inner = unsafe { NonNull::new_unchecked(list) };
Ok(Self { inner })
}
NIXL_CAPI_ERROR_INVALID_PARAM => Err(NixlError::InvalidParam),
_ => Err(NixlError::BackendError),
}
}
pub fn len(&self) -> Result<usize, NixlError> {
let mut size = 0;
let status = unsafe { nixl_capi_query_resp_list_size(self.inner.as_ptr(), &mut size) };
match status {
NIXL_CAPI_SUCCESS => Ok(size),
NIXL_CAPI_ERROR_INVALID_PARAM => Err(NixlError::InvalidParam),
_ => Err(NixlError::BackendError),
}
}
pub fn is_empty(&self) -> Result<bool, NixlError> {
Ok(self.len()? == 0)
}
pub fn get(&self, index: usize) -> Result<QueryResponse<'_>, NixlError> {
let size = self.len()?;
if index >= size {
return Err(NixlError::InvalidParam);
}
Ok(QueryResponse { list: self, index })
}
pub fn iter(&self) -> Result<QueryResponseIterator<'_>, NixlError> {
Ok(QueryResponseIterator {
list: self,
index: 0,
len: self.len()?,
})
}
pub(crate) fn handle(&self) -> *mut bindings::nixl_capi_query_resp_list_s {
self.inner.as_ptr()
}
}
impl<'a> QueryResponse<'a> {
pub fn has_value(&self) -> Result<bool, NixlError> {
let mut has_value = false;
let status = unsafe {
nixl_capi_query_resp_list_has_value(
self.list.inner.as_ptr(),
self.index,
&mut has_value,
)
};
match status {
NIXL_CAPI_SUCCESS => Ok(has_value),
NIXL_CAPI_ERROR_INVALID_PARAM => Err(NixlError::InvalidParam),
_ => Err(NixlError::BackendError),
}
}
pub fn get_params(&self) -> Result<Option<Params>, NixlError> {
if !self.has_value()? {
return Ok(None);
}
let mut params = ptr::null_mut();
let status = unsafe {
nixl_capi_query_resp_list_get_params(self.list.inner.as_ptr(), self.index, &mut params)
};
match status {
NIXL_CAPI_SUCCESS => {
let inner = unsafe { NonNull::new_unchecked(params) };
Ok(Some(Params::new(inner)))
}
NIXL_CAPI_ERROR_INVALID_PARAM => Err(NixlError::InvalidParam),
_ => Err(NixlError::BackendError),
}
}
}
pub struct QueryResponseIterator<'a> {
list: &'a QueryResponseList,
index: usize,
len: usize,
}
impl<'a> Iterator for QueryResponseIterator<'a> {
type Item = QueryResponse<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.len {
None
} else {
let response = QueryResponse {
list: self.list,
index: self.index,
};
self.index += 1;
Some(response)
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len - self.index;
(remaining, Some(remaining))
}
}
impl<'a> ExactSizeIterator for QueryResponseIterator<'a> {
fn len(&self) -> usize {
self.len - self.index
}
}
impl Drop for QueryResponseList {
fn drop(&mut self) {
unsafe {
nixl_capi_destroy_query_resp_list(self.inner.as_ptr());
}
}
}