Skip to content

Commit

Permalink
impl some traits
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkMyCar committed Feb 2, 2024
1 parent 6095596 commit 7b1bb93
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ impl Message for () {

pub mod str {
use ::bytes::{Buf, BufMut, Bytes};
use core::hash::Hash;
use core::mem;
use core::ops::Deref;
use core::str::Utf8Error;
Expand All @@ -433,9 +434,9 @@ pub mod str {
use crate::error::DecodeError;
use crate::Message;

#[derive(Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct Checked;
#[derive(Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct Unchecked;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -527,6 +528,44 @@ pub mod str {
}
}

impl<T, S: AsRef<str>> PartialEq<S> for ByteStr<T> {
fn eq(&self, other: &S) -> bool {
self.as_str() == other.as_ref()
}
}

impl<T> PartialEq<ByteStr<T>> for String {
fn eq(&self, other: &ByteStr<T>) -> bool {
self.as_str() == other.as_str()
}
}

impl<T> PartialEq<ByteStr<T>> for &str {
fn eq(&self, other: &ByteStr<T>) -> bool {
*self == other.as_str()
}
}

impl<T> Eq for ByteStr<T> {}

impl<T> Ord for ByteStr<T> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}

impl<T> PartialOrd for ByteStr<T> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl<T> Hash for ByteStr<T> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state)
}
}

impl Message for ByteStr {
fn encode_raw<B>(&self, buf: &mut B)
where
Expand Down

0 comments on commit 7b1bb93

Please sign in to comment.