use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Body {
data: Vec<u8>,
}
impl Body {
#[must_use]
pub const fn empty() -> Self {
Self { data: Vec::new() }
}
#[must_use]
pub const fn from_bytes(data: Vec<u8>) -> Self {
Self { data }
}
#[must_use]
pub const fn from_string(s: String) -> Self {
Self { data: s.into_bytes() }
}
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.data
}
#[must_use]
pub const fn len(&self) -> usize {
self.data.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[must_use]
pub fn into_bytes(self) -> Vec<u8> {
self.data
}
pub fn to_string(&self) -> Result<String, alloc::string::FromUtf8Error> {
String::from_utf8(self.data.clone())
}
pub fn into_string(self) -> Result<String, alloc::string::FromUtf8Error> {
String::from_utf8(self.data)
}
#[must_use]
pub const fn as_bytes_mut(&mut self) -> &mut Vec<u8> {
&mut self.data
}
}
impl From<Vec<u8>> for Body {
fn from(data: Vec<u8>) -> Self {
Self::from_bytes(data)
}
}
impl From<String> for Body {
fn from(s: String) -> Self {
Self::from_string(s)
}
}
impl From<&str> for Body {
fn from(s: &str) -> Self {
Self::from_string(String::from(s))
}
}
impl AsRef<[u8]> for Body {
fn as_ref(&self) -> &[u8] {
&self.data
}
}