rustis 0.19.3

Redis async driver for Rust
Documentation
use crate::resp::{deserialize_byte_buf, serialize_byte_buf};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::{fmt, ops::Deref};

/// Represents the [Bulk String](https://redis.io/docs/reference/protocol-spec/#resp-bulk-strings) RESP type
#[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 {
    /// Constructs a new `BulkString` from a bytes buffer
    #[inline]
    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
        Self(bytes.into())
    }

    /// Returns the internal buffer as a byte slice
    #[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())
    }
}

/// Represents a reference to the [Bulk String](https://redis.io/docs/reference/protocol-spec/#resp-bulk-strings) RESP type
#[derive(Serialize, Hash, PartialEq, Eq, Clone)]
pub struct RefBulkString<'a>(#[serde(serialize_with = "serialize_byte_buf")] &'a [u8]);

impl<'a> RefBulkString<'a> {
    /// Constructs a new `RefBulkString` from a byte slice
    #[inline]
    pub fn new(bytes: &'a [u8]) -> Self {
        Self(bytes)
    }

    /// Returns the internal buffer as a byte slice
    #[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())
    }
}