use {IntoBuf, Buf, BufMut};
use buf::Iter;
use std::{cmp, fmt, mem, hash, ops, slice, ptr, usize};
use std::borrow::Borrow;
use std::io::Cursor;
use std::sync::atomic::{self, AtomicUsize, AtomicPtr};
use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
pub struct Bytes {
inner: Inner2,
}
pub struct BytesMut {
inner: Inner2,
}
#[cfg(target_endian = "little")]
#[repr(C)]
struct Inner {
arc: AtomicPtr<Shared>,
ptr: *mut u8,
len: usize,
cap: usize,
}
#[cfg(target_endian = "big")]
#[repr(C)]
struct Inner {
ptr: *mut u8,
len: usize,
cap: usize,
arc: AtomicPtr<Shared>,
}
struct Inner2 {
inner: Inner,
}
struct Shared {
vec: Vec<u8>,
original_capacity: usize,
ref_count: AtomicUsize,
}
const KIND_ARC: usize = 0b00;
const KIND_INLINE: usize = 0b01;
const KIND_STATIC: usize = 0b10;
const KIND_VEC: usize = 0b11;
const KIND_MASK: usize = 0b11;
const MAX_ORIGINAL_CAPACITY: usize = 1 << 16;
const INLINE_LEN_MASK: usize = 0b11111100;
const INLINE_LEN_OFFSET: usize = 2;
#[cfg(target_endian = "little")]
const INLINE_DATA_OFFSET: isize = 1;
#[cfg(target_endian = "big")]
const INLINE_DATA_OFFSET: isize = 0;
#[cfg(target_pointer_width = "64")]
const INLINE_CAP: usize = 4 * 8 - 1;
#[cfg(target_pointer_width = "32")]
const INLINE_CAP: usize = 4 * 4 - 1;
impl Bytes {
#[inline]
pub fn new() -> Bytes {
Bytes {
inner: Inner2 {
inner: Inner::empty(),
}
}
}
#[inline]
pub fn from_static(bytes: &'static [u8]) -> Bytes {
Bytes {
inner: Inner2 {
inner: Inner::from_static(bytes),
}
}
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn slice(&self, begin: usize, end: usize) -> Bytes {
let mut ret = self.clone();
unsafe {
ret.inner.set_end(end);
ret.inner.set_start(begin);
}
ret
}
pub fn slice_from(&self, begin: usize) -> Bytes {
self.slice(begin, self.len())
}
pub fn slice_to(&self, end: usize) -> Bytes {
self.slice(0, end)
}
pub fn split_off(&mut self, at: usize) -> Bytes {
Bytes {
inner: Inner2 {
inner: self.inner.split_off(at),
}
}
}
pub fn split_to(&mut self, at: usize) -> Bytes {
Bytes {
inner: Inner2 {
inner: self.inner.split_to(at),
}
}
}
#[deprecated(since = "0.4.1", note = "use split_to instead")]
#[doc(hidden)]
pub fn drain_to(&mut self, at: usize) -> Bytes {
self.split_to(at)
}
pub fn try_mut(mut self) -> Result<BytesMut, Bytes> {
if self.inner.is_mut_safe() {
Ok(BytesMut { inner: self.inner })
} else {
Err(self)
}
}
}
impl IntoBuf for Bytes {
type Buf = Cursor<Self>;
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
}
}
impl<'a> IntoBuf for &'a Bytes {
type Buf = Cursor<Self>;
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
}
}
impl Clone for Bytes {
fn clone(&self) -> Bytes {
Bytes {
inner: Inner2 {
inner: self.inner.shallow_clone(),
}
}
}
}
impl AsRef<[u8]> for Bytes {
fn as_ref(&self) -> &[u8] {
self.inner.as_ref()
}
}
impl ops::Deref for Bytes {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.inner.as_ref()
}
}
impl From<BytesMut> for Bytes {
fn from(src: BytesMut) -> Bytes {
src.freeze()
}
}
impl From<Vec<u8>> for Bytes {
fn from(src: Vec<u8>) -> Bytes {
BytesMut::from(src).freeze()
}
}
impl From<String> for Bytes {
fn from(src: String) -> Bytes {
BytesMut::from(src).freeze()
}
}
impl<'a> From<&'a [u8]> for Bytes {
fn from(src: &'a [u8]) -> Bytes {
BytesMut::from(src).freeze()
}
}
impl<'a> From<&'a str> for Bytes {
fn from(src: &'a str) -> Bytes {
BytesMut::from(src).freeze()
}
}
impl PartialEq for Bytes {
fn eq(&self, other: &Bytes) -> bool {
self.inner.as_ref() == other.inner.as_ref()
}
}
impl PartialOrd for Bytes {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
self.inner.as_ref().partial_cmp(other.inner.as_ref())
}
}
impl Ord for Bytes {
fn cmp(&self, other: &Bytes) -> cmp::Ordering {
self.inner.as_ref().cmp(other.inner.as_ref())
}
}
impl Eq for Bytes {
}
impl fmt::Debug for Bytes {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.inner.as_ref(), fmt)
}
}
impl hash::Hash for Bytes {
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {
let s: &[u8] = self.as_ref();
s.hash(state);
}
}
impl Borrow<[u8]> for Bytes {
fn borrow(&self) -> &[u8] {
self.as_ref()
}
}
impl IntoIterator for Bytes {
type Item = u8;
type IntoIter = Iter<Cursor<Bytes>>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
}
}
impl<'a> IntoIterator for &'a Bytes {
type Item = u8;
type IntoIter = Iter<Cursor<&'a Bytes>>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
}
}
impl BytesMut {
#[inline]
pub fn with_capacity(capacity: usize) -> BytesMut {
BytesMut {
inner: Inner2 {
inner: Inner::with_capacity(capacity),
},
}
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
#[inline]
pub fn freeze(self) -> Bytes {
Bytes { inner: self.inner }
}
pub fn split_off(&mut self, at: usize) -> BytesMut {
BytesMut {
inner: Inner2 {
inner: self.inner.split_off(at),
}
}
}
pub fn take(&mut self) -> BytesMut {
let len = self.len();
self.split_to(len)
}
#[deprecated(since = "0.4.1", note = "use take instead")]
#[doc(hidden)]
pub fn drain(&mut self) -> BytesMut {
self.take()
}
pub fn split_to(&mut self, at: usize) -> BytesMut {
BytesMut {
inner: Inner2 {
inner: self.inner.split_to(at),
}
}
}
#[deprecated(since = "0.4.1", note = "use split_to instead")]
#[doc(hidden)]
pub fn drain_to(&mut self, at: usize) -> BytesMut {
self.split_to(at)
}
pub fn truncate(&mut self, len: usize) {
if len <= self.len() {
unsafe { self.set_len(len); }
}
}
pub fn clear(&mut self) {
self.truncate(0);
}
pub unsafe fn set_len(&mut self, len: usize) {
self.inner.set_len(len)
}
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
}
impl BufMut for BytesMut {
#[inline]
fn remaining_mut(&self) -> usize {
self.capacity() - self.len()
}
#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
let new_len = self.len() + cnt;
self.inner.set_len(new_len);
}
#[inline]
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
let len = self.len();
&mut self.inner.as_raw()[len..]
}
#[inline]
fn put_slice(&mut self, src: &[u8]) {
assert!(self.remaining_mut() >= src.len());
let len = src.len();
unsafe {
self.bytes_mut()[..len].copy_from_slice(src);
self.advance_mut(len);
}
}
}
impl IntoBuf for BytesMut {
type Buf = Cursor<Self>;
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
}
}
impl<'a> IntoBuf for &'a BytesMut {
type Buf = Cursor<&'a BytesMut>;
fn into_buf(self) -> Self::Buf {
Cursor::new(self)
}
}
impl AsRef<[u8]> for BytesMut {
fn as_ref(&self) -> &[u8] {
self.inner.as_ref()
}
}
impl ops::Deref for BytesMut {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_ref()
}
}
impl ops::DerefMut for BytesMut {
fn deref_mut(&mut self) -> &mut [u8] {
self.inner.as_mut()
}
}
impl From<Vec<u8>> for BytesMut {
fn from(src: Vec<u8>) -> BytesMut {
BytesMut {
inner: Inner2 {
inner: Inner::from_vec(src),
},
}
}
}
impl From<String> for BytesMut {
fn from(src: String) -> BytesMut {
BytesMut::from(src.into_bytes())
}
}
impl<'a> From<&'a [u8]> for BytesMut {
fn from(src: &'a [u8]) -> BytesMut {
let len = src.len();
if len <= INLINE_CAP {
unsafe {
let mut inner: Inner = mem::uninitialized();
inner.arc = AtomicPtr::new(KIND_INLINE as *mut Shared);
inner.set_inline_len(len);
inner.as_raw()[0..len].copy_from_slice(src);
BytesMut {
inner: Inner2 {
inner: inner,
}
}
}
} else {
let mut buf = BytesMut::with_capacity(src.len());
let src: &[u8] = src.as_ref();
buf.put(src);
buf
}
}
}
impl<'a> From<&'a str> for BytesMut {
fn from(src: &'a str) -> BytesMut {
BytesMut::from(src.as_bytes())
}
}
impl From<Bytes> for BytesMut {
fn from(src: Bytes) -> BytesMut {
src.try_mut()
.unwrap_or_else(|src| BytesMut::from(&src[..]))
}
}
impl PartialEq for BytesMut {
fn eq(&self, other: &BytesMut) -> bool {
self.inner.as_ref() == other.inner.as_ref()
}
}
impl PartialOrd for BytesMut {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
self.inner.as_ref().partial_cmp(other.inner.as_ref())
}
}
impl Ord for BytesMut {
fn cmp(&self, other: &BytesMut) -> cmp::Ordering {
self.inner.as_ref().cmp(other.inner.as_ref())
}
}
impl Eq for BytesMut {
}
impl fmt::Debug for BytesMut {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.inner.as_ref(), fmt)
}
}
impl hash::Hash for BytesMut {
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {
let s: &[u8] = self.as_ref();
s.hash(state);
}
}
impl Borrow<[u8]> for BytesMut {
fn borrow(&self) -> &[u8] {
self.as_ref()
}
}
impl fmt::Write for BytesMut {
fn write_str(&mut self, s: &str) -> fmt::Result {
BufMut::put(self, s);
Ok(())
}
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
fmt::write(self, args)
}
}
impl Clone for BytesMut {
fn clone(&self) -> BytesMut {
BytesMut::from(&self[..])
}
}
impl IntoIterator for BytesMut {
type Item = u8;
type IntoIter = Iter<Cursor<BytesMut>>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
}
}
impl<'a> IntoIterator for &'a BytesMut {
type Item = u8;
type IntoIter = Iter<Cursor<&'a BytesMut>>;
fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
}
}
impl Extend<u8> for BytesMut {
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item = u8> {
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
self.reserve(lower);
for b in iter {
unsafe {
self.bytes_mut()[0] = b;
self.advance_mut(1);
}
}
}
}
impl<'a> Extend<&'a u8> for BytesMut {
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item = &'a u8> {
self.extend(iter.into_iter().map(|b| *b))
}
}
impl Inner {
#[inline]
fn empty() -> Inner {
Inner {
arc: AtomicPtr::new(KIND_VEC as *mut Shared),
ptr: ptr::null_mut(),
len: 0,
cap: 0,
}
}
#[inline]
fn from_static(bytes: &'static [u8]) -> Inner {
let ptr = bytes.as_ptr() as *mut u8;
Inner {
arc: AtomicPtr::new(KIND_STATIC as *mut Shared),
ptr: ptr,
len: bytes.len(),
cap: bytes.len(),
}
}
#[inline]
fn from_vec(mut src: Vec<u8>) -> Inner {
let len = src.len();
let cap = src.capacity();
let ptr = src.as_mut_ptr();
mem::forget(src);
let original_capacity = cmp::min(cap, MAX_ORIGINAL_CAPACITY);
let arc = (original_capacity & !KIND_MASK) | KIND_VEC;
Inner {
arc: AtomicPtr::new(arc as *mut Shared),
ptr: ptr,
len: len,
cap: cap,
}
}
#[inline]
fn with_capacity(capacity: usize) -> Inner {
if capacity <= INLINE_CAP {
unsafe {
Inner {
arc: AtomicPtr::new(KIND_INLINE as *mut Shared),
.. mem::uninitialized()
}
}
} else {
Inner::from_vec(Vec::with_capacity(capacity))
}
}
#[inline]
fn as_ref(&self) -> &[u8] {
unsafe {
if self.is_inline() {
slice::from_raw_parts(self.inline_ptr(), self.inline_len())
} else {
slice::from_raw_parts(self.ptr, self.len)
}
}
}
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
debug_assert!(!self.is_static());
unsafe {
if self.is_inline() {
slice::from_raw_parts_mut(self.inline_ptr(), self.inline_len())
} else {
slice::from_raw_parts_mut(self.ptr, self.len)
}
}
}
#[inline]
unsafe fn as_raw(&mut self) -> &mut [u8] {
debug_assert!(!self.is_static());
if self.is_inline() {
slice::from_raw_parts_mut(self.inline_ptr(), INLINE_CAP)
} else {
slice::from_raw_parts_mut(self.ptr, self.cap)
}
}
#[inline]
fn len(&self) -> usize {
if self.is_inline() {
self.inline_len()
} else {
self.len
}
}
#[inline]
unsafe fn inline_ptr(&self) -> *mut u8 {
(self as *const Inner as *mut Inner as *mut u8)
.offset(INLINE_DATA_OFFSET)
}
#[inline]
fn inline_len(&self) -> usize {
let p: &usize = unsafe { mem::transmute(&self.arc) };
(p & INLINE_LEN_MASK) >> INLINE_LEN_OFFSET
}
#[inline]
fn set_inline_len(&mut self, len: usize) {
debug_assert!(len <= INLINE_CAP);
let p: &mut usize = unsafe { mem::transmute(&mut self.arc) };
*p = (*p & !INLINE_LEN_MASK) | (len << INLINE_LEN_OFFSET);
}
#[inline]
unsafe fn set_len(&mut self, len: usize) {
if self.is_inline() {
assert!(len <= INLINE_CAP);
self.set_inline_len(len);
} else {
assert!(len <= self.cap);
self.len = len;
}
}
#[inline]
fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
fn capacity(&self) -> usize {
if self.is_inline() {
INLINE_CAP
} else {
self.cap
}
}
fn split_off(&mut self, at: usize) -> Inner {
let mut other = self.shallow_clone();
unsafe {
other.set_start(at);
self.set_end(at);
}
return other
}
fn split_to(&mut self, at: usize) -> Inner {
let mut other = self.shallow_clone();
unsafe {
other.set_end(at);
self.set_start(at);
}
return other
}
unsafe fn set_start(&mut self, start: usize) {
debug_assert!(self.is_shared());
if start == 0 {
return;
}
if self.is_inline() {
assert!(start <= INLINE_CAP);
let len = self.inline_len();
if len <= start {
self.set_inline_len(0);
} else {
let new_len = len - start;
let dst = self.inline_ptr();
let src = (dst as *const u8).offset(start as isize);
ptr::copy(src, dst, new_len);
self.set_inline_len(new_len);
}
} else {
assert!(start <= self.cap);
self.ptr = self.ptr.offset(start as isize);
if self.len >= start {
self.len -= start;
} else {
self.len = 0;
}
self.cap -= start;
}
}
unsafe fn set_end(&mut self, end: usize) {
debug_assert!(self.is_shared());
if self.is_inline() {
assert!(end <= INLINE_CAP);
let new_len = cmp::min(self.inline_len(), end);
self.set_inline_len(new_len);
} else {
assert!(end <= self.cap);
self.cap = end;
self.len = cmp::min(self.len, end);
}
}
fn is_mut_safe(&mut self) -> bool {
let kind = self.kind();
if kind == KIND_INLINE {
true
} else if kind == KIND_VEC {
true
} else if kind == KIND_STATIC {
false
} else {
let arc = self.arc.load(Relaxed);
unsafe { (*arc).is_unique() }
}
}
fn shallow_clone(&self) -> Inner {
if self.is_inline() {
unsafe {
let mut inner: Inner = mem::uninitialized();
let len = self.inline_len();
inner.arc = AtomicPtr::new(KIND_INLINE as *mut Shared);
inner.set_inline_len(len);
inner.as_raw()[0..len].copy_from_slice(self.as_ref());
inner
}
} else {
let mut arc = self.arc.load(Acquire);
if arc as usize & KIND_MASK == KIND_VEC {
unsafe {
let shared = Box::new(Shared {
vec: Vec::from_raw_parts(self.ptr, self.len, self.cap),
original_capacity: arc as usize & !KIND_MASK,
ref_count: AtomicUsize::new(2),
});
let shared = Box::into_raw(shared);
debug_assert!(0 == (shared as usize & 0b11));
let actual = self.arc.compare_and_swap(arc, shared, AcqRel);
if actual == arc {
return Inner {
arc: AtomicPtr::new(shared),
.. *self
};
}
let shared: Box<Shared> = mem::transmute(shared);
mem::forget(*shared);
arc = actual;
}
} else if arc as usize & KIND_MASK == KIND_STATIC {
return Inner {
arc: AtomicPtr::new(arc),
.. *self
};
}
unsafe {
let old_size = (*arc).ref_count.fetch_add(1, Relaxed);
if old_size == usize::MAX {
panic!(); }
}
Inner {
arc: AtomicPtr::new(arc),
.. *self
}
}
}
#[inline]
fn reserve(&mut self, additional: usize) {
let len = self.len();
let rem = self.capacity() - len;
if additional <= rem {
return;
}
let kind = self.kind();
if kind == KIND_INLINE {
let new_cap = len + additional;
let mut v = Vec::with_capacity(new_cap);
v.extend_from_slice(self.as_ref());
self.ptr = v.as_mut_ptr();
self.len = v.len();
self.cap = v.capacity();
self.arc = AtomicPtr::new(KIND_VEC as *mut Shared);
mem::forget(v);
return;
}
if kind == KIND_VEC {
unsafe {
let mut v = Vec::from_raw_parts(self.ptr, self.len, self.cap);
v.reserve(additional);
self.ptr = v.as_mut_ptr();
self.len = v.len();
self.cap = v.capacity();
mem::forget(v);
return;
}
}
let arc = self.arc.load(Relaxed);
debug_assert!(kind == KIND_ARC);
let mut new_cap = len + additional;
let original_capacity;
unsafe {
original_capacity = (*arc).original_capacity;
if (*arc).is_unique() {
let v = &mut (*arc).vec;
if v.capacity() >= new_cap {
let ptr = v.as_mut_ptr();
ptr::copy(self.ptr, ptr, len);
self.ptr = ptr;
self.cap = v.capacity();
return;
}
new_cap = cmp::max(
cmp::max(v.capacity() << 1, new_cap),
original_capacity);
} else {
new_cap = cmp::max(new_cap, original_capacity);
}
}
let mut v = Vec::with_capacity(new_cap.next_power_of_two());
v.extend_from_slice(self.as_ref());
release_shared(arc);
self.ptr = v.as_mut_ptr();
self.len = v.len();
self.cap = v.capacity();
let arc = (original_capacity & !KIND_MASK) | KIND_VEC;
self.arc = AtomicPtr::new(arc as *mut Shared);
mem::forget(v);
}
#[inline]
fn is_inline(&self) -> bool {
self.kind() == KIND_INLINE
}
#[inline]
fn is_shared(&mut self) -> bool {
match self.kind() {
KIND_INLINE | KIND_ARC => true,
_ => false,
}
}
#[inline]
fn is_static(&mut self) -> bool {
match self.kind() {
KIND_STATIC => true,
_ => false,
}
}
#[inline]
fn kind(&self) -> usize {
#[cfg(target_endian = "little")]
#[inline]
fn imp(arc: &AtomicPtr<Shared>) -> usize {
unsafe {
let p: &u8 = mem::transmute(arc);
(*p as usize) & KIND_MASK
}
}
#[cfg(target_endian = "big")]
#[inline]
fn imp(arc: &AtomicPtr<Shared>) -> usize {
unsafe {
let p: &usize = mem::transmute(arc);
*p & KIND_MASK
}
}
imp(&self.arc)
}
}
impl Drop for Inner2 {
fn drop(&mut self) {
let kind = self.kind();
if kind == KIND_VEC {
unsafe {
let _ = Vec::from_raw_parts(self.ptr, self.len, self.cap);
}
} else if kind == KIND_ARC {
let arc = self.arc.load(Relaxed);
release_shared(arc);
}
}
}
fn release_shared(ptr: *mut Shared) {
unsafe {
if (*ptr).ref_count.fetch_sub(1, Release) != 1 {
return;
}
atomic::fence(Acquire);
let _: Box<Shared> = mem::transmute(ptr);
}
}
impl Shared {
fn is_unique(&self) -> bool {
self.ref_count.load(Acquire) == 1
}
}
unsafe impl Send for Inner {}
unsafe impl Sync for Inner {}
impl ops::Deref for Inner2 {
type Target = Inner;
fn deref(&self) -> &Inner {
&self.inner
}
}
impl ops::DerefMut for Inner2 {
fn deref_mut(&mut self) -> &mut Inner {
&mut self.inner
}
}
impl PartialEq<[u8]> for BytesMut {
fn eq(&self, other: &[u8]) -> bool {
&**self == other
}
}
impl PartialOrd<[u8]> for BytesMut {
fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> {
(**self).partial_cmp(other)
}
}
impl PartialEq<BytesMut> for [u8] {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialOrd<BytesMut> for [u8] {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<str> for BytesMut {
fn eq(&self, other: &str) -> bool {
&**self == other.as_bytes()
}
}
impl PartialOrd<str> for BytesMut {
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
(**self).partial_cmp(other.as_bytes())
}
}
impl PartialEq<BytesMut> for str {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialOrd<BytesMut> for str {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<Vec<u8>> for BytesMut {
fn eq(&self, other: &Vec<u8>) -> bool {
*self == &other[..]
}
}
impl PartialOrd<Vec<u8>> for BytesMut {
fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> {
(**self).partial_cmp(&other[..])
}
}
impl PartialEq<BytesMut> for Vec<u8> {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialOrd<BytesMut> for Vec<u8> {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<String> for BytesMut {
fn eq(&self, other: &String) -> bool {
*self == &other[..]
}
}
impl PartialOrd<String> for BytesMut {
fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> {
(**self).partial_cmp(other.as_bytes())
}
}
impl PartialEq<BytesMut> for String {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialOrd<BytesMut> for String {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut
where BytesMut: PartialEq<T>
{
fn eq(&self, other: &&'a T) -> bool {
*self == **other
}
}
impl<'a, T: ?Sized> PartialOrd<&'a T> for BytesMut
where BytesMut: PartialOrd<T>
{
fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> {
self.partial_cmp(*other)
}
}
impl<'a> PartialEq<BytesMut> for &'a [u8] {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl<'a> PartialOrd<BytesMut> for &'a [u8] {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl<'a> PartialEq<BytesMut> for &'a str {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl<'a> PartialOrd<BytesMut> for &'a str {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<[u8]> for Bytes {
fn eq(&self, other: &[u8]) -> bool {
self.inner.as_ref() == other
}
}
impl PartialOrd<[u8]> for Bytes {
fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> {
self.inner.as_ref().partial_cmp(other)
}
}
impl PartialEq<Bytes> for [u8] {
fn eq(&self, other: &Bytes) -> bool {
*other == *self
}
}
impl PartialOrd<Bytes> for [u8] {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<str> for Bytes {
fn eq(&self, other: &str) -> bool {
self.inner.as_ref() == other.as_bytes()
}
}
impl PartialOrd<str> for Bytes {
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
self.inner.as_ref().partial_cmp(other.as_bytes())
}
}
impl PartialEq<Bytes> for str {
fn eq(&self, other: &Bytes) -> bool {
*other == *self
}
}
impl PartialOrd<Bytes> for str {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<Vec<u8>> for Bytes {
fn eq(&self, other: &Vec<u8>) -> bool {
*self == &other[..]
}
}
impl PartialOrd<Vec<u8>> for Bytes {
fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> {
self.inner.as_ref().partial_cmp(&other[..])
}
}
impl PartialEq<Bytes> for Vec<u8> {
fn eq(&self, other: &Bytes) -> bool {
*other == *self
}
}
impl PartialOrd<Bytes> for Vec<u8> {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<String> for Bytes {
fn eq(&self, other: &String) -> bool {
*self == &other[..]
}
}
impl PartialOrd<String> for Bytes {
fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> {
self.inner.as_ref().partial_cmp(other.as_bytes())
}
}
impl PartialEq<Bytes> for String {
fn eq(&self, other: &Bytes) -> bool {
*other == *self
}
}
impl PartialOrd<Bytes> for String {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl<'a> PartialEq<Bytes> for &'a [u8] {
fn eq(&self, other: &Bytes) -> bool {
*other == *self
}
}
impl<'a> PartialOrd<Bytes> for &'a [u8] {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl<'a> PartialEq<Bytes> for &'a str {
fn eq(&self, other: &Bytes) -> bool {
*other == *self
}
}
impl<'a> PartialOrd<Bytes> for &'a str {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes
where Bytes: PartialEq<T>
{
fn eq(&self, other: &&'a T) -> bool {
*self == **other
}
}
impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes
where Bytes: PartialOrd<T>
{
fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> {
self.partial_cmp(&**other)
}
}