From df3e3edc8a40b959aa21dd1544cdfbae8a6d61f0 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 13 Jan 2016 15:14:24 +0100 Subject: [PATCH] bytes_ref --- src/bytes.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/bytes.rs b/src/bytes.rs index 8c56d6bf3..e925d4509 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -39,6 +39,7 @@ use std::fmt; use std::slice; use std::cmp::Ordering; use std::error::Error as StdError; +use std::ops::{Deref, DerefMut}; use uint::{U128, U256}; use hash::FixedHash; @@ -89,6 +90,31 @@ impl ToPretty for Bytes { } } +pub enum BytesRef<'a> { + Flexible(&'a mut Bytes), + Fixed(&'a mut [u8]) +} + +impl<'a> Deref for BytesRef<'a> { + type Target = [u8]; + + fn deref(&self) -> &[u8] { + match self { + &BytesRef::Flexible(ref bytes) => bytes, + &BytesRef::Fixed(ref bytes) => bytes + } + } +} + +impl <'a> DerefMut for BytesRef<'a> { + fn deref_mut(&mut self) -> &mut [u8] { + match self { + &mut BytesRef::Flexible(ref mut bytes) => bytes, + &mut BytesRef::Fixed(ref mut bytes) => bytes + } + } +} + /// Vector of bytes pub type Bytes = Vec; @@ -436,4 +462,4 @@ fn populate_big_types() { let mut h = h256_from_u64(0x69); h.copy_raw_from(&a); assert_eq!(h, h256_from_hex("ffffffffffffffffffffffffffffffffffffffff000000000000000000000069")); -} \ No newline at end of file +}