1use crate::__internal::{Private, SealedInternal};
9use crate::{AsView, IntoProxied, IntoView, ProtoBytes, ProtoStr, ProtoString, Proxied, View};
10use paste::paste;
11use std::cmp::PartialEq;
12use std::ops::Deref;
13
14macro_rules! impl_cord_types {
15 ($($t:ty, $vt:ty);*) => {
16 paste! { $(
17 #[derive(Debug)]
18 pub struct [< $t Cord>](Private);
19
20 #[derive(Debug)]
21 pub enum [< $t Cow>]<'a> {
22 Borrowed(View<'a, $t>),
23 Owned($t),
24 }
25
26 impl SealedInternal for [< $t Cord>] {}
27
28 impl<'msg> SealedInternal for [< $t Cow>]<'msg> {}
29
30 impl Proxied for [< $t Cord>] {
31 type View<'msg> = [< $t Cow>]<'msg>;
32 }
33
34 impl AsView for [< $t Cord>] {
35 type Proxied = Self;
36 fn as_view(&self) -> [< $t Cow>]<'_> {
37 unimplemented!("Proto Cord should never be constructed");
38 }
39 }
40
41 impl<'msg> AsView for [< $t Cow>]<'msg> {
42 type Proxied = [< $t Cord>];
43
44 fn as_view(&self) -> [< $t Cow>]<'_> {
45 match self {
46 [< $t Cow>]::Owned(owned) => [< $t Cow>]::Borrowed((*owned).as_view()),
47 [< $t Cow>]::Borrowed(borrowed) => [< $t Cow>]::Borrowed(borrowed),
48 }
49 }
50 }
51
52 impl<'msg> IntoView<'msg> for [< $t Cow>]<'msg> {
53 fn into_view<'shorter>(self) -> [< $t Cow>]<'shorter>
54 where
55 'msg: 'shorter, {
56 match self {
57 [< $t Cow>]::Owned(owned) => [< $t Cow>]::Owned(owned),
58 [< $t Cow>]::Borrowed(borrow) => [< $t Cow>]::Borrowed(borrow.into_view()),
59 }
60 }
61 }
62
63 impl IntoProxied<$t> for [< $t Cow>]<'_> {
64 fn into_proxied(self, _private: Private) -> $t {
65 match self {
66 [< $t Cow>]::Owned(owned) => owned,
67 [< $t Cow>]::Borrowed(borrowed) => borrowed.into_proxied(Private),
68 }
69 }
70 }
71
72 impl<'a> Deref for [< $t Cow>]<'a> {
73 type Target = $vt;
74
75 fn deref(&self) -> View<'_, $t> {
76 match self {
77 [< $t Cow>]::Borrowed(borrow) => borrow,
78 [< $t Cow>]::Owned(owned) => (*owned).as_view(),
79 }
80 }
81 }
82
83 impl AsRef<[u8]> for [< $t Cow>]<'_> {
84 fn as_ref(&self) -> &[u8] {
85 match self {
86 [< $t Cow>]::Borrowed(borrow) => borrow.as_ref(),
87 [< $t Cow>]::Owned(owned) => owned.as_ref(),
88 }
89 }
90 }
91 )*
92 }
93 }
94}
95
96impl_cord_types!(
97 ProtoString, ProtoStr;
98 ProtoBytes, [u8]
99);
100
101macro_rules! impl_eq {
102 ($($t1:ty, $t2:ty);*) => {
103 paste! { $(
104 impl PartialEq<$t1> for $t2 {
105 fn eq(&self, rhs: &$t1) -> bool {
106 AsRef::<[u8]>::as_ref(self) == AsRef::<[u8]>::as_ref(rhs)
107 }
108 }
109 )*
110 }
111 }
112}
113
114impl_eq!(
115 ProtoStringCow<'_>, ProtoStringCow<'_>;
116 str, ProtoStringCow<'_>;
117 ProtoStringCow<'_>, str;
118 ProtoBytesCow<'_>, ProtoBytesCow<'_>;
119 [u8], ProtoBytesCow<'_>;
120 ProtoBytesCow<'_>, [u8]
121);