tor-bytes: Add initial support for the bytes crate

This commit is contained in:
Nick Mathewson 2020-09-05 10:06:58 -04:00
parent b679ab216c
commit 8db525dab4
3 changed files with 17 additions and 0 deletions

View File

@ -14,6 +14,7 @@ generic-array = "0.14.4"
crypto-mac = "0.9.1"
thiserror = "1.0.20"
signature = "1.2.2"
bytes = "*"
[dev-dependencies]
hex-literal = "0.3.1"

View File

@ -5,6 +5,7 @@
//! this is where I'm putting them.
use super::*;
use bytes;
use generic_array::GenericArray;
// ----------------------------------------------------------------------
@ -25,6 +26,12 @@ impl Writer for Vec<u8> {
}
}
impl Writer for bytes::BytesMut {
fn write_all(&mut self, bytes: &[u8]) {
self.extend_from_slice(bytes);
}
}
// ----------------------------------------------------------------------
impl<'a> Writeable for [u8] {

View File

@ -1,5 +1,6 @@
use crate::{Error, Readable, Result};
use arrayref::array_ref;
use bytes;
/// A type for reading messages from a slice of bytes.
///
@ -54,6 +55,14 @@ impl<'a> Reader<'a> {
pub fn from_slice(slice: &'a [u8]) -> Self {
Reader { b: slice, off: 0 }
}
/// Construct a new Reader from a 'Bytes' object.
pub fn from_bytes(b: &'a bytes::Bytes) -> Self {
Self::from_slice(b.as_ref())
}
/// Construct a new Reader from a 'BytesMut' object
pub fn from_bytes_mut(b: &'a bytes::BytesMut) -> Self {
Self::from_slice(b.as_ref())
}
/// Return the total length of the slice in this reader, including
/// consumed bytes and remaining bytes.
pub fn total_len(&self) -> usize {