refactor(uri): make ByteStr an implementation detail of uri

This commit is contained in:
Yazad Daruvala
2017-06-23 21:52:56 -07:00
parent 579d360f51
commit 3021cd9dd8
6 changed files with 11 additions and 22 deletions

3
src/common/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub use self::str::ByteStr;
mod str;

45
src/common/str.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::ops::Deref;
use std::str;
use bytes::Bytes;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ByteStr(Bytes);
impl ByteStr {
pub unsafe fn from_utf8_unchecked(slice: Bytes) -> ByteStr {
ByteStr(slice)
}
pub fn from_static(s: &'static str) -> ByteStr {
ByteStr(Bytes::from_static(s.as_bytes()))
}
pub fn slice(&self, from: usize, to: usize) -> ByteStr {
assert!(self.as_str().is_char_boundary(from));
assert!(self.as_str().is_char_boundary(to));
ByteStr(self.0.slice(from, to))
}
pub fn slice_to(&self, idx: usize) -> ByteStr {
assert!(self.as_str().is_char_boundary(idx));
ByteStr(self.0.slice_to(idx))
}
pub fn as_str(&self) -> &str {
unsafe { str::from_utf8_unchecked(self.0.as_ref()) }
}
}
impl Deref for ByteStr {
type Target = str;
fn deref(&self) -> &str {
self.as_str()
}
}
impl<'a> From<&'a str> for ByteStr {
fn from(s: &'a str) -> ByteStr {
ByteStr(Bytes::from(s))
}
}