From c9d3427fd5ded91d0f13b161020316287af97bb4 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 8 Apr 2015 10:51:52 -0700 Subject: [PATCH] Move various ByteStr impls into separate files --- src/lib.rs | 1 + src/str/mod.rs | 6 +- src/str/seq.rs | 100 +++++++++++++++++++++++++++ src/str/{byte_str.rs => small.rs} | 108 +----------------------------- 4 files changed, 107 insertions(+), 108 deletions(-) create mode 100644 src/str/seq.rs rename src/str/{byte_str.rs => small.rs} (51%) diff --git a/src/lib.rs b/src/lib.rs index 3fce9ce..2c53582 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![crate_name = "bytes"] #![unstable] +#![deny(warnings)] pub mod alloc; mod buf; diff --git a/src/str/mod.rs b/src/str/mod.rs index 7afd544..0a830a7 100644 --- a/src/str/mod.rs +++ b/src/str/mod.rs @@ -1,10 +1,12 @@ -mod byte_str; mod bytes; mod rope; +mod seq; +mod small; -pub use self::byte_str::{SeqByteStr, SmallByteStr, SmallByteStrBuf}; pub use self::bytes::Bytes; pub use self::rope::{Rope, RopeBuf}; +pub use self::seq::SeqByteStr; +pub use self::small::{SmallByteStr, SmallByteStrBuf}; use {Buf}; use std::{cmp, fmt, ops}; diff --git a/src/str/seq.rs b/src/str/seq.rs new file mode 100644 index 0000000..a8ab256 --- /dev/null +++ b/src/str/seq.rs @@ -0,0 +1,100 @@ +use {alloc, ByteBuf, MutBufExt, ByteStr, ROByteBuf, Rope, Bytes, ToBytes}; +use std::ops; + +pub struct SeqByteStr { + mem: alloc::MemRef, + pos: u32, + len: u32, +} + +impl SeqByteStr { + /// Create a new `SeqByteStr` from a byte slice. + /// + /// The contents of the byte slice will be copied. + pub fn from_slice(bytes: &[u8]) -> SeqByteStr { + let mut buf = ByteBuf::mut_with_capacity(bytes.len()); + + if let Err(e) = buf.write(bytes) { + panic!("failed to copy bytes from slice; err={:?}", e); + } + + buf.flip().to_seq_byte_str() + } + + /// Creates a new `SeqByteStr` from a `MemRef`, an offset, and a length. + /// + /// This function is unsafe as there are no guarantees that the given + /// arguments are valid. + pub unsafe fn from_mem_ref(mem: alloc::MemRef, pos: u32, len: u32) -> SeqByteStr { + SeqByteStr { + mem: mem, + pos: pos, + len: len, + } + } +} + +impl ByteStr for SeqByteStr { + type Buf = ROByteBuf; + + fn buf(&self) -> ROByteBuf { + unsafe { + let pos = self.pos; + let lim = pos + self.len; + + ROByteBuf::from_mem_ref(self.mem.clone(), lim, pos, lim) + } + } + + fn concat(&self, other: &B) -> Bytes { + Rope::of(self.clone()).concat(other) + } + + fn len(&self) -> usize { + self.len as usize + } + + fn slice(&self, begin: usize, end: usize) -> Bytes { + if begin >= end || begin >= self.len() { + return Bytes::empty() + } + + let bytes = unsafe { + SeqByteStr::from_mem_ref( + self.mem.clone(), + self.pos + begin as u32, + (end - begin) as u32) + }; + + Bytes::of(bytes) + } +} + +impl ToBytes for SeqByteStr { + fn to_bytes(self) -> Bytes { + Bytes::of(self) + } +} + +impl ops::Index for SeqByteStr { + type Output = u8; + + fn index(&self, index: usize) -> &u8 { + assert!(index < self.len()); + + unsafe { + &*self.mem.ptr() + .offset(index as isize + self.pos as isize) + } + } +} + +impl Clone for SeqByteStr { + fn clone(&self) -> SeqByteStr { + SeqByteStr { + mem: self.mem.clone(), + pos: self.pos, + len: self.len, + } + } +} diff --git a/src/str/byte_str.rs b/src/str/small.rs similarity index 51% rename from src/str/byte_str.rs rename to src/str/small.rs index 886ecc9..949ac1b 100644 --- a/src/str/byte_str.rs +++ b/src/str/small.rs @@ -1,111 +1,7 @@ -use {alloc, Bytes, ByteBuf, ROByteBuf, Rope}; -use traits::{Buf, MutBuf, MutBufExt, ByteStr, ToBytes}; +use {Bytes, Rope}; +use traits::{Buf, MutBuf, ByteStr, ToBytes}; use std::{cmp, ops}; -/* - * - * ===== SeqByteStr ===== - * - */ - -pub struct SeqByteStr { - mem: alloc::MemRef, - pos: u32, - len: u32, -} - -impl SeqByteStr { - /// Create a new `SeqByteStr` from a byte slice. - /// - /// The contents of the byte slice will be copied. - pub fn from_slice(bytes: &[u8]) -> SeqByteStr { - let mut buf = ByteBuf::mut_with_capacity(bytes.len()); - - if let Err(e) = buf.write(bytes) { - panic!("failed to copy bytes from slice; err={:?}", e); - } - - buf.flip().to_seq_byte_str() - } - - /// Creates a new `SeqByteStr` from a `MemRef`, an offset, and a length. - /// - /// This function is unsafe as there are no guarantees that the given - /// arguments are valid. - pub unsafe fn from_mem_ref(mem: alloc::MemRef, pos: u32, len: u32) -> SeqByteStr { - SeqByteStr { - mem: mem, - pos: pos, - len: len, - } - } -} - -impl ByteStr for SeqByteStr { - type Buf = ROByteBuf; - - fn buf(&self) -> ROByteBuf { - unsafe { - let pos = self.pos; - let lim = pos + self.len; - - ROByteBuf::from_mem_ref(self.mem.clone(), lim, pos, lim) - } - } - - fn concat(&self, other: &B) -> Bytes { - Rope::of(self.clone()).concat(other) - } - - fn len(&self) -> usize { - self.len as usize - } - - fn slice(&self, begin: usize, end: usize) -> Bytes { - if begin >= end || begin >= self.len() { - return Bytes::empty() - } - - let bytes = unsafe { - SeqByteStr::from_mem_ref( - self.mem.clone(), - self.pos + begin as u32, - (end - begin) as u32) - }; - - Bytes::of(bytes) - } -} - -impl ToBytes for SeqByteStr { - fn to_bytes(self) -> Bytes { - Bytes::of(self) - } -} - -impl ops::Index for SeqByteStr { - type Output = u8; - - fn index(&self, index: usize) -> &u8 { - assert!(index < self.len()); - - unsafe { - &*self.mem.ptr() - .offset(index as isize + self.pos as isize) - } - } -} - -impl Clone for SeqByteStr { - fn clone(&self) -> SeqByteStr { - SeqByteStr { - mem: self.mem.clone(), - pos: self.pos, - len: self.len, - } - } -} - /* * * ===== SmallByteStr =====