use crate::*;
impl PartialEq for ArcRwLockStream {
fn eq(&self, other: &Self) -> bool {
Arc::as_ptr(self.get_0()) == Arc::as_ptr(other.get_0())
}
}
impl Eq for ArcRwLockStream {}
impl ArcRwLockStream {
#[inline(always)]
pub fn from(arc_rw_lock_stream: ArcRwLock<TcpStream>) -> Self {
Self(arc_rw_lock_stream)
}
#[inline(always)]
pub fn from_stream(stream: TcpStream) -> Self {
Self(arc_rwlock(stream))
}
pub async fn read(&'_ self) -> RwLockReadGuard<'_, TcpStream> {
self.get_0().read().await
}
pub(crate) async fn write(&'_ self) -> RwLockWriteGuard<'_, TcpStream> {
self.get_0().write().await
}
pub async fn try_send<D>(&self, data: D) -> Result<(), ResponseError>
where
D: AsRef<[u8]>,
{
Ok(self.write().await.write_all(data.as_ref()).await?)
}
pub async fn send<D>(&self, data: D)
where
D: AsRef<[u8]>,
{
self.try_send(data).await.unwrap();
}
pub async fn try_send_body<D>(&self, data: D) -> Result<(), ResponseError>
where
D: AsRef<[u8]>,
{
Ok(self.write().await.write_all(data.as_ref()).await?)
}
pub async fn send_body<D>(&self, data: D)
where
D: AsRef<[u8]>,
{
self.try_send_body(data).await.unwrap();
}
pub async fn try_send_body_list<I, D>(&self, data_iter: I) -> Result<(), ResponseError>
where
I: IntoIterator<Item = D>,
D: AsRef<[u8]>,
{
let mut stream: RwLockWriteGuard<'_, TcpStream> = self.write().await;
for data in data_iter {
stream.write_all(data.as_ref()).await?;
}
Ok(())
}
pub async fn send_body_list<I, D>(&self, data_iter: I)
where
I: IntoIterator<Item = D>,
D: AsRef<[u8]>,
{
self.try_send_body_list(data_iter).await.unwrap();
}
pub async fn try_flush(&self) -> Result<(), ResponseError> {
Ok(self.write().await.flush().await?)
}
pub async fn flush(&self) {
self.try_flush().await.unwrap();
}
}