#![allow(dead_code)]
#![allow(unused)]
use crate::__internal::runtime::InnerProtoString;
use crate::__internal::{Private, SealedInternal};
use crate::{AsView, IntoProxied, IntoView, Mut, MutProxied, Optional, Proxied, View};
use std::borrow::Cow;
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::convert::{AsMut, AsRef};
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter;
use std::ops::{Deref, DerefMut};
use std::ptr;
use std::rc::Rc;
use std::sync::Arc;
pub struct ProtoBytes {
pub(crate) inner: InnerProtoString,
}
impl ProtoBytes {
#[doc(hidden)]
pub fn into_inner(self, _private: Private) -> InnerProtoString {
self.inner
}
#[doc(hidden)]
pub fn from_inner(_private: Private, inner: InnerProtoString) -> ProtoBytes {
Self { inner }
}
pub fn as_view(&self) -> &[u8] {
self.inner.as_bytes()
}
}
impl AsRef<[u8]> for ProtoBytes {
fn as_ref(&self) -> &[u8] {
self.inner.as_bytes()
}
}
impl From<&[u8]> for ProtoBytes {
fn from(v: &[u8]) -> ProtoBytes {
ProtoBytes { inner: InnerProtoString::from(v) }
}
}
impl<const N: usize> From<&[u8; N]> for ProtoBytes {
fn from(v: &[u8; N]) -> ProtoBytes {
ProtoBytes { inner: InnerProtoString::from(v.as_ref()) }
}
}
impl SealedInternal for ProtoBytes {}
impl Proxied for ProtoBytes {
type View<'msg> = &'msg [u8];
}
impl AsView for ProtoBytes {
type Proxied = Self;
fn as_view(&self) -> &[u8] {
self.as_view()
}
}
impl IntoProxied<ProtoBytes> for &[u8] {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(self)
}
}
impl<const N: usize> IntoProxied<ProtoBytes> for &[u8; N] {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(self.as_ref())
}
}
impl IntoProxied<ProtoBytes> for Vec<u8> {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(AsRef::<[u8]>::as_ref(&self))
}
}
impl IntoProxied<ProtoBytes> for &Vec<u8> {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(AsRef::<[u8]>::as_ref(self))
}
}
impl IntoProxied<ProtoBytes> for Box<[u8]> {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(AsRef::<[u8]>::as_ref(&self))
}
}
impl IntoProxied<ProtoBytes> for Cow<'_, [u8]> {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(AsRef::<[u8]>::as_ref(&self))
}
}
impl IntoProxied<ProtoBytes> for Rc<[u8]> {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(AsRef::<[u8]>::as_ref(&self))
}
}
impl IntoProxied<ProtoBytes> for Arc<[u8]> {
fn into_proxied(self, _private: Private) -> ProtoBytes {
ProtoBytes::from(AsRef::<[u8]>::as_ref(&self))
}
}
impl SealedInternal for &[u8] {}
impl AsView for &[u8] {
type Proxied = ProtoBytes;
fn as_view(&self) -> &[u8] {
self
}
}
impl<'msg> IntoView<'msg> for &'msg [u8] {
fn into_view<'shorter>(self) -> &'shorter [u8]
where
'msg: 'shorter,
{
self
}
}
#[derive(Debug, PartialEq)]
pub struct Utf8Error {
pub(crate) inner: std::str::Utf8Error,
}
impl std::fmt::Display for Utf8Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl std::error::Error for Utf8Error {}
impl From<std::str::Utf8Error> for Utf8Error {
fn from(inner: std::str::Utf8Error) -> Utf8Error {
Utf8Error { inner }
}
}
pub struct ProtoString {
pub(crate) inner: InnerProtoString,
}
impl ProtoString {
pub fn as_view(&self) -> &ProtoStr {
unsafe { ProtoStr::from_utf8_unchecked(self.as_bytes()) }
}
pub fn as_bytes(&self) -> &[u8] {
self.inner.as_bytes()
}
#[doc(hidden)]
pub fn into_inner(self, _private: Private) -> InnerProtoString {
self.inner
}
#[doc(hidden)]
pub fn from_inner(_private: Private, inner: InnerProtoString) -> ProtoString {
Self { inner }
}
}
impl SealedInternal for ProtoString {}
impl AsRef<[u8]> for ProtoString {
fn as_ref(&self) -> &[u8] {
self.inner.as_bytes()
}
}
impl From<ProtoString> for ProtoBytes {
fn from(v: ProtoString) -> Self {
ProtoBytes { inner: v.inner }
}
}
impl From<&str> for ProtoString {
fn from(v: &str) -> Self {
Self::from(v.as_bytes())
}
}
impl From<&[u8]> for ProtoString {
fn from(v: &[u8]) -> Self {
Self { inner: InnerProtoString::from(v) }
}
}
impl SealedInternal for &str {}
impl SealedInternal for &ProtoStr {}
impl IntoProxied<ProtoString> for &str {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(self)
}
}
impl IntoProxied<ProtoString> for &ProtoStr {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(self.as_bytes())
}
}
impl IntoProxied<ProtoString> for String {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(self.as_str())
}
}
impl IntoProxied<ProtoString> for &String {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(self.as_bytes())
}
}
impl IntoProxied<ProtoString> for OsString {
fn into_proxied(self, private: Private) -> ProtoString {
self.as_os_str().into_proxied(private)
}
}
impl IntoProxied<ProtoString> for &OsStr {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(self.as_encoded_bytes())
}
}
impl IntoProxied<ProtoString> for Box<str> {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(AsRef::<str>::as_ref(&self))
}
}
impl IntoProxied<ProtoString> for Cow<'_, str> {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(AsRef::<str>::as_ref(&self))
}
}
impl IntoProxied<ProtoString> for Rc<str> {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(AsRef::<str>::as_ref(&self))
}
}
impl IntoProxied<ProtoString> for Arc<str> {
fn into_proxied(self, _private: Private) -> ProtoString {
ProtoString::from(AsRef::<str>::as_ref(&self))
}
}
#[repr(transparent)]
pub struct ProtoStr([u8]);
impl ProtoStr {
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn to_str(&self) -> Result<&str, Utf8Error> {
Ok(std::str::from_utf8(&self.0)?)
}
pub fn to_cow_lossy(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.0)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub unsafe fn from_utf8_unchecked(bytes: &[u8]) -> &Self {
unsafe { &*(bytes as *const [u8] as *const Self) }
}
pub fn from_str(string: &str) -> &Self {
unsafe { Self::from_utf8_unchecked(string.as_bytes()) }
}
}
impl AsRef<[u8]> for ProtoStr {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl<'msg> From<&'msg ProtoStr> for &'msg [u8] {
fn from(val: &'msg ProtoStr) -> &'msg [u8] {
val.as_bytes()
}
}
impl<'msg> From<&'msg str> for &'msg ProtoStr {
fn from(val: &'msg str) -> &'msg ProtoStr {
ProtoStr::from_str(val)
}
}
impl<'msg> TryFrom<&'msg ProtoStr> for &'msg str {
type Error = Utf8Error;
fn try_from(val: &'msg ProtoStr) -> Result<&'msg str, Utf8Error> {
val.to_str()
}
}
impl<'msg> TryFrom<&'msg [u8]> for &'msg ProtoStr {
type Error = Utf8Error;
fn try_from(val: &'msg [u8]) -> Result<&'msg ProtoStr, Utf8Error> {
Ok(ProtoStr::from_str(std::str::from_utf8(val)?))
}
}
impl fmt::Debug for ProtoStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\"");
for chunk in self.as_bytes().utf8_chunks() {
for ch in chunk.valid().chars() {
write!(f, "{}", ch.escape_debug());
}
for byte in chunk.invalid() {
write!(f, "\\x{:02X}", byte);
}
}
write!(f, "\"");
Ok(())
}
}
impl fmt::Display for ProtoStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&String::from_utf8_lossy(self.as_bytes()), f)?;
Ok(())
}
}
impl Hash for ProtoStr {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_bytes().hash(state)
}
}
impl Eq for ProtoStr {}
impl Ord for ProtoStr {
fn cmp(&self, other: &ProtoStr) -> Ordering {
self.as_bytes().cmp(other.as_bytes())
}
}
impl Proxied for ProtoString {
type View<'msg> = &'msg ProtoStr;
}
impl AsView for ProtoString {
type Proxied = Self;
fn as_view(&self) -> &ProtoStr {
self.as_view()
}
}
impl AsView for &ProtoStr {
type Proxied = ProtoString;
fn as_view(&self) -> &ProtoStr {
self
}
}
impl<'msg> IntoView<'msg> for &'msg ProtoStr {
fn into_view<'shorter>(self) -> &'shorter ProtoStr
where
'msg: 'shorter,
{
self
}
}
macro_rules! impl_bytes_partial_cmp {
($(<($($generics:tt)*)> $lhs:ty => $rhs:ty),+ $(,)?) => {
$(
impl<$($generics)*> PartialEq<$rhs> for $lhs {
fn eq(&self, other: &$rhs) -> bool {
AsRef::<[u8]>::as_ref(self) == AsRef::<[u8]>::as_ref(other)
}
}
impl<$($generics)*> PartialOrd<$rhs> for $lhs {
fn partial_cmp(&self, other: &$rhs) -> Option<Ordering> {
AsRef::<[u8]>::as_ref(self).partial_cmp(AsRef::<[u8]>::as_ref(other))
}
}
)*
};
}
impl_bytes_partial_cmp!(
<()> ProtoStr => ProtoStr,
<()> ProtoStr => str,
<()> str => ProtoStr,
);
impl std::fmt::Debug for ProtoString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
std::fmt::Debug::fmt(self.as_view(), f)
}
}
impl std::fmt::Debug for ProtoBytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
std::fmt::Debug::fmt(self.as_view(), f)
}
}
unsafe impl Sync for ProtoString {}
unsafe impl Send for ProtoString {}
unsafe impl Send for ProtoBytes {}
unsafe impl Sync for ProtoBytes {}
#[cfg(test)]
mod tests {
use super::*;
use googletest::prelude::*;
fn test_proto_str(bytes: &[u8]) -> &ProtoStr {
unsafe { ProtoStr::from_utf8_unchecked(bytes) }
}
}