use crate::*;
impl Response {
pub fn from<T>(data: T) -> Self
where
T: Into<ResponseData>,
{
Self(data.into())
}
pub fn get_data(&self) -> &ResponseData {
&self.0
}
pub fn get_mut_data(&mut self) -> &mut ResponseData {
&mut self.0
}
pub fn set_data<T>(&mut self, data: T) -> &mut Self
where
T: Into<ResponseData>,
{
self.0 = data.into();
self
}
pub fn clear(&mut self) -> &mut Self {
self.0.clear();
self
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub async fn try_send(&self, stream: &ArcRwLockStream) -> ResponseResult {
stream.try_send(&self.0).await
}
pub async fn send(&self, stream: &ArcRwLockStream) {
self.try_send(stream).await.unwrap();
}
pub async fn try_flush(&self, stream: &ArcRwLockStream) -> ResponseResult {
stream.try_flush().await
}
pub async fn flush(&self, stream: &ArcRwLockStream) {
self.try_flush(stream).await.unwrap();
}
pub async fn try_close(&self, stream: &ArcRwLockStream) -> ResponseResult {
stream.try_shutdown().await
}
pub async fn close(&self, stream: &ArcRwLockStream) {
self.try_close(stream).await.unwrap();
}
}
impl From<ResponseData> for Response {
fn from(data: ResponseData) -> Self {
Self(data)
}
}
impl From<&[u8]> for Response {
fn from(data: &[u8]) -> Self {
Self(data.to_owned())
}
}
impl From<String> for Response {
fn from(data: String) -> Self {
Self(data.into_bytes())
}
}
impl From<&str> for Response {
fn from(data: &str) -> Self {
Self(data.as_bytes().to_owned())
}
}