use std;
use ByteStr;
#[derive(Clone, Default, PartialEq, Eq)]
pub struct ByteString {
inner: Vec<u8>,
}
impl ByteString {
#[inline]
pub fn new() -> Self {
Self { inner: Vec::new() }
}
#[inline]
pub fn from_vec(vec: Vec<u8>) -> Self {
Self { inner: vec }
}
#[inline]
pub fn from_slice(slice: &[u8]) -> Self {
Self::from_vec(slice.to_vec())
}
#[inline]
pub fn as_vec(&self) -> &Vec<u8> {
&self.inner
}
#[inline]
pub fn as_mut_vec(&mut self) -> &mut Vec<u8> {
&mut self.inner
}
#[inline]
pub fn into_vec(self) -> Vec<u8> {
self.inner
}
#[inline]
pub fn into_boxed_slice(self) -> Box<[u8]> {
self.into_vec().into_boxed_slice()
}
#[inline]
pub fn into_boxed_byte_str(self) -> Box<ByteStr> {
unsafe { Box::from_raw(Box::into_raw(self.into_boxed_slice()) as *mut ByteStr) }
}
#[inline]
pub fn as_byte_str(&self) -> &ByteStr {
ByteStr::from_slice(self.as_vec().as_slice())
}
#[inline]
pub fn as_mut_byte_str(&mut self) -> &mut ByteStr {
ByteStr::from_slice_mut(self.as_mut_vec().as_mut_slice())
}
#[inline]
pub fn capacity(&self) -> usize {
self.as_vec().capacity()
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.as_mut_vec().reserve(additional);
}
#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.as_mut_vec().reserve_exact(additional);
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.as_mut_vec().shrink_to_fit();
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.as_mut_vec().truncate(len);
}
#[inline]
pub fn clear(&mut self) {
self.as_mut_vec().clear();
}
#[inline]
pub unsafe fn set_len(&mut self, len: usize) {
self.as_mut_vec().set_len(len);
}
#[inline]
pub fn swap_remove(&mut self, index: usize) -> u8 {
self.as_mut_vec().swap_remove(index)
}
#[inline]
pub fn insert(&mut self, index: usize, element: u8) {
self.as_mut_vec().insert(index, element);
}
#[inline]
pub fn remove(&mut self, index: usize) -> u8 {
self.as_mut_vec().remove(index)
}
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where F: FnMut(u8) -> bool
{
self.as_mut_vec().retain(|&byte| f(byte));
}
#[inline]
pub fn dedup_by_key<F, K>(&mut self, key: F)
where F: FnMut(&mut u8) -> K,
K: PartialEq<K>
{
self.as_mut_vec().dedup_by_key(key);
}
#[inline]
pub fn dedup_by<F>(&mut self, same_bucket: F)
where F: FnMut(&mut u8, &mut u8) -> bool
{
self.as_mut_vec().dedup_by(same_bucket);
}
#[inline]
pub fn dedup(&mut self) {
self.as_mut_vec().dedup();
}
#[inline]
pub fn push(&mut self, element: u8) {
self.as_mut_vec().push(element);
}
#[inline]
pub fn pop(&mut self) -> Option<u8> {
self.as_mut_vec().pop()
}
#[inline]
pub fn push_slice(&mut self, other: &[u8]) {
self.as_mut_vec().extend_from_slice(other);
}
#[inline]
pub fn push_byte_str(&mut self, other: &ByteStr) {
self.push_slice(other.as_slice());
}
pub fn insert_slice(&mut self, index: usize, other: &[u8]) {
let old_len = self.len();
assert!(index <= old_len);
self.reserve(other.len());
unsafe {
std::ptr::copy(self.as_ptr().offset(index as isize),
self.as_mut_ptr().offset((index + other.len()) as isize),
old_len - index);
std::ptr::copy_nonoverlapping(other.as_ptr(), self.as_mut_ptr().offset(index as isize), other.len());
}
}
#[inline]
pub fn insert_byte_str(&mut self, index: usize, other: &ByteStr) {
self.insert_slice(index, other.as_slice());
}
#[inline]
pub fn drain(&mut self, start: Option<usize>, end: Option<usize>) -> std::vec::Drain<u8> {
match (start, end) {
(None, None) => self.as_mut_vec().drain(..),
(Some(start), None) => self.as_mut_vec().drain(start ..),
(None, Some(end)) => self.as_mut_vec().drain(.. end),
(Some(start), Some(end)) => self.as_mut_vec().drain(start .. end),
}
}
#[inline]
pub fn split_off(&mut self, at: usize) -> Self {
Self::from_vec(self.as_mut_vec().split_off(at))
}
}
impl<'a> From<&'a [u8]> for ByteString {
#[inline]
fn from(src: &'a [u8]) -> Self {
ByteString::from_slice(src)
}
}
impl<'a> From<&'a ByteStr> for ByteString {
#[inline]
fn from(src: &'a ByteStr) -> Self {
src.to_byte_string()
}
}
impl<'a> From<&'a str> for ByteString {
#[inline]
fn from(src: &'a str) -> Self {
ByteString::from_slice(src.as_bytes())
}
}
impl From<Vec<u8>> for ByteString {
#[inline]
fn from(src: Vec<u8>) -> Self {
ByteString::from_vec(src)
}
}
impl From<Box<[u8]>> for ByteString {
#[inline]
fn from(src: Box<[u8]>) -> Self {
ByteString::from_vec(Vec::from(src))
}
}
impl From<Box<ByteStr>> for ByteString {
#[inline]
fn from(src: Box<ByteStr>) -> Self {
src.into_byte_string()
}
}
impl From<Box<str>> for ByteString {
#[inline]
fn from(src: Box<str>) -> Self {
ByteString::from(Box::<[u8]>::from(src))
}
}
impl From<String> for ByteString {
#[inline]
fn from(src: String) -> Self {
ByteString::from(src.into_bytes())
}
}
impl std::iter::FromIterator<u8> for ByteString {
#[inline]
fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item=u8>
{
ByteString::from_vec(std::iter::FromIterator::from_iter(iter))
}
}
impl std::fmt::Debug for ByteString {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
std::fmt::Debug::fmt(self.as_byte_str(), f)
}
}
impl std::ops::Deref for ByteString {
type Target = ByteStr;
#[inline]
fn deref(&self) -> &ByteStr {
self.as_byte_str()
}
}
impl std::ops::DerefMut for ByteString {
#[inline]
fn deref_mut(&mut self) -> &mut ByteStr {
self.as_mut_byte_str()
}
}
impl std::borrow::Borrow<ByteStr> for ByteString {
#[inline]
fn borrow(&self) -> &ByteStr {
self.as_byte_str()
}
}
impl std::borrow::BorrowMut<ByteStr> for ByteString {
#[inline]
fn borrow_mut(&mut self) -> &mut ByteStr {
self.as_mut_byte_str()
}
}
impl std::borrow::Borrow<[u8]> for ByteString {
#[inline]
fn borrow(&self) -> &[u8] {
self.as_slice()
}
}
impl std::borrow::BorrowMut<[u8]> for ByteString {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
self.as_mut_slice()
}
}
impl std::convert::AsRef<ByteStr> for ByteString {
#[inline]
fn as_ref(&self) -> &ByteStr {
self.as_byte_str()
}
}
impl std::convert::AsRef<[u8]> for ByteString {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl std::convert::AsMut<ByteStr> for ByteString {
#[inline]
fn as_mut(&mut self) -> &mut ByteStr {
self.as_mut_byte_str()
}
}
impl std::convert::AsMut<[u8]> for ByteString {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
self.as_mut_slice()
}
}