use alloc::boxed::Box;
#[cfg(feature = "remote")]
use alloc::vec::Vec;
use core::future::poll_fn;
use crate::buffer::BufferReader;
use crate::DbError;
#[cfg(feature = "remote")]
use crate::buffer::JsonBufferReader;
pub struct Reader<T: Clone + Send> {
inner: Box<dyn BufferReader<T> + Send>,
}
impl<T: Clone + Send> Reader<T> {
pub fn new(inner: Box<dyn BufferReader<T> + Send>) -> Self {
Self { inner }
}
pub async fn recv(&mut self) -> Result<T, DbError> {
poll_fn(|cx| self.inner.poll_recv(cx)).await
}
pub fn try_recv(&mut self) -> Result<T, DbError> {
self.inner.try_recv()
}
}
#[cfg(feature = "remote")]
pub struct JsonReader {
inner: Box<dyn JsonBufferReader + Send>,
}
#[cfg(feature = "remote")]
impl JsonReader {
pub fn new(inner: Box<dyn JsonBufferReader + Send>) -> Self {
Self { inner }
}
pub async fn recv_json(&mut self) -> Result<serde_json::Value, DbError> {
poll_fn(|cx| self.inner.poll_recv_json(cx)).await
}
pub async fn recv_json_bytes(&mut self) -> Result<Vec<u8>, DbError> {
poll_fn(|cx| self.inner.poll_recv_json_bytes(cx)).await
}
pub fn try_recv_json(&mut self) -> Result<serde_json::Value, DbError> {
self.inner.try_recv_json()
}
pub fn try_recv_json_bytes(&mut self) -> Result<Vec<u8>, DbError> {
self.inner.try_recv_json_bytes()
}
}