#![deny(warnings, missing_docs, missing_debug_implementations)]
#![doc(html_root_url = "https://docs.rs/string/0.2.1")]
#[cfg(feature = "bytes")]
extern crate bytes;
use std::{borrow, fmt, hash, ops, str};
use std::default::Default;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct String<T = Vec<u8>> {
value: T,
}
impl<T> String<T> {
pub fn get_ref(&self) -> &T {
&self.value
}
pub unsafe fn get_mut(&mut self) -> &mut T {
&mut self.value
}
pub fn into_inner(self) -> T {
self.value
}
pub fn from_str<'a>(src: &'a str) -> String<T>
where T: From<&'a [u8]> + StableAsRef,
{
let value: T = src.as_bytes().into();
Self { value }
}
}
impl String {
pub fn new() -> String {
String::default()
}
}
impl<T> String<T>
where T: AsRef<[u8]>,
{
pub unsafe fn from_utf8_unchecked(value: T) -> String<T> {
String { value }
}
}
impl<T> PartialEq<str> for String<T>
where T: AsRef<[u8]>
{
fn eq(&self, other: &str) -> bool {
&self[..] == other
}
}
impl<T> hash::Hash for String<T>
where T: AsRef<[u8]>
{
fn hash<H: hash::Hasher>(&self, state: &mut H) {
ops::Deref::deref(self).hash(state);
}
}
impl<T> ops::Deref for String<T>
where T: AsRef<[u8]>
{
type Target = str;
#[inline]
fn deref(&self) -> &str {
let b = self.value.as_ref();
unsafe { str::from_utf8_unchecked(b) }
}
}
impl<T> ops::DerefMut for String<T>
where T: AsRef<[u8]> + AsMut<[u8]>
{
#[inline]
fn deref_mut(&mut self) -> &mut str {
let b = self.value.as_mut();
unsafe { str::from_utf8_unchecked_mut(b) }
}
}
impl<T> borrow::Borrow<str> for String<T>
where T: AsRef<[u8]>
{
fn borrow(&self) -> &str {
&*self
}
}
impl From<::std::string::String> for String<::std::string::String> {
fn from(value: ::std::string::String) -> Self {
String { value }
}
}
impl<T> Default for String<T>
where T: Default + StableAsRef
{
fn default() -> Self {
String { value: T::default() }
}
}
impl<T> TryFrom<T> for String<T>
where T: AsRef<[u8]> + StableAsRef
{
type Error = str::Utf8Error;
fn try_from(value: T) -> Result<Self, Self::Error> {
let _ = str::from_utf8(value.as_ref())?;
Ok(String { value })
}
}
impl<T: AsRef<[u8]>> fmt::Debug for String<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
(**self).fmt(fmt)
}
}
impl<T: AsRef<[u8]>> fmt::Display for String<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
(**self).fmt(fmt)
}
}
pub trait TryFrom<T>: Sized + sealed::Sealed {
type Error;
fn try_from(value: T) -> Result<Self, Self::Error>;
}
impl<T> sealed::Sealed for String<T> {}
mod sealed {
pub trait Sealed {}
}
pub unsafe trait StableAsRef {}
unsafe impl<'a, T> StableAsRef for &'a T where T: StableAsRef {}
unsafe impl<'a, T> StableAsRef for &'a mut T where T: StableAsRef {}
unsafe impl<T> StableAsRef for Box<T> where T: StableAsRef {}
unsafe impl<T> StableAsRef for std::rc::Rc<T> where T: StableAsRef {}
unsafe impl<T> StableAsRef for std::sync::Arc<T> where T: StableAsRef {}
unsafe impl StableAsRef for std::string::String {}
unsafe impl StableAsRef for str {}
unsafe impl StableAsRef for Vec<u8> {}
unsafe impl StableAsRef for [u8] {}
#[cfg(feature = "bytes")]
unsafe impl StableAsRef for bytes::Bytes {}
#[cfg(feature = "bytes")]
unsafe impl StableAsRef for bytes::BytesMut {}
macro_rules! array_impls {
($($len:expr)+) => {
$(
unsafe impl StableAsRef for [u8; $len] {}
)+
}
}
array_impls!(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16);
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_from_std_string() {
let s: String<_> = "hello".to_string().into();
assert_eq!(&s, "hello");
}
#[test]
fn test_from_str() {
let _: String<Vec<u8>> = String::from_str("nice str");
}
#[test]
fn test_try_from_bytes() {
let _ = String::try_from(b"nice bytes").unwrap();
}
}