Implement Hash and Borrow for Bytes / BytesMut

This commit is contained in:
Carl Lerche
2017-02-16 16:52:56 -08:00
parent 646624c130
commit 268226051d
+28 -1
View File
@@ -1,6 +1,7 @@
use {IntoBuf, BufMut};
use std::{cmp, fmt, mem, ops, slice, ptr};
use std::{cmp, fmt, mem, hash, ops, slice, ptr};
use std::borrow::Borrow;
use std::cell::{Cell, UnsafeCell};
use std::io::Cursor;
use std::sync::Arc;
@@ -522,6 +523,19 @@ impl fmt::Debug for Bytes {
}
}
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()
}
}
unsafe impl Sync for Bytes {}
/*
@@ -1003,6 +1017,19 @@ impl fmt::Debug for BytesMut {
}
}
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);