use crate::resp::{deserialize_byte_buf, serialize_byte_buf};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Deref};
#[derive(Deserialize, Serialize, Hash, PartialEq, Eq, Clone)]
pub struct BulkString(
#[serde(
deserialize_with = "deserialize_byte_buf",
serialize_with = "serialize_byte_buf"
)]
Vec<u8>,
);
impl BulkString {
#[inline]
pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
Self(bytes.into())
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
impl Deref for BulkString {
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<BulkString> for Vec<u8> {
#[inline]
fn from(bs: BulkString) -> Self {
bs.0
}
}
impl From<Vec<u8>> for BulkString {
#[inline]
fn from(bytes: Vec<u8>) -> Self {
BulkString(bytes)
}
}
impl From<Bytes> for BulkString {
#[inline]
fn from(bytes: Bytes) -> Self {
BulkString(bytes.to_vec())
}
}
impl<const N: usize> From<&[u8; N]> for BulkString {
#[inline]
fn from(bytes: &[u8; N]) -> Self {
BulkString(bytes.to_vec())
}
}
impl fmt::Debug for BulkString {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("BulkString").field(&self.0).finish()
}
}
impl fmt::Display for BulkString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(String::from_utf8_lossy(&self.0).as_ref())
}
}
#[derive(Serialize, Hash, PartialEq, Eq, Clone)]
pub struct RefBulkString<'a>(#[serde(serialize_with = "serialize_byte_buf")] &'a [u8]);
impl<'a> RefBulkString<'a> {
#[inline]
pub fn new(bytes: &'a [u8]) -> Self {
Self(bytes)
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
self.0
}
}
impl<'a> Deref for RefBulkString<'a> {
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
self.0
}
}
impl<'a> From<&'a [u8]> for RefBulkString<'a> {
#[inline]
fn from(bytes: &'a [u8]) -> Self {
Self(bytes)
}
}
impl<'a, const N: usize> From<&'a [u8; N]> for RefBulkString<'a> {
#[inline]
fn from(bytes: &'a [u8; N]) -> Self {
Self(bytes.as_slice())
}
}
impl<'a> fmt::Debug for RefBulkString<'a> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("RefBulkString").field(&self.0).finish()
}
}
impl<'a> fmt::Display for RefBulkString<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(String::from_utf8_lossy(self.0).as_ref())
}
}