1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2024 Google LLC. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
8 use crate::__internal::{Private, SealedInternal};
10 AsView, IntoProxied, IntoView, ProtoBytes, ProtoStr, ProtoString, Proxied, Proxy, View,
14 use std::cmp::PartialEq;
17 macro_rules! impl_cord_types {
18 ($($t:ty, $vt:ty);*) => {
21 pub struct [< $t Cord>](Private);
24 pub enum [< $t Cow>]<'a> {
25 Borrowed(View<'a, $t>),
29 impl SealedInternal for [< $t Cord>] {}
31 impl<'msg> SealedInternal for [< $t Cow>]<'msg> {}
33 impl Proxied for [< $t Cord>] {
34 type View<'msg> = [< $t Cow>]<'msg>;
37 impl AsView for [< $t Cord>] {
39 fn as_view(&self) -> [< $t Cow>]<'_> {
40 unimplemented!("Proto Cord should never be constructed");
44 impl<'msg> Proxy<'msg> for [< $t Cow>]<'msg> {}
46 impl<'msg> ViewProxy<'msg> for [< $t Cow>]<'msg> {}
48 impl<'msg> AsView for [< $t Cow>]<'msg> {
49 type Proxied = [< $t Cord>];
51 fn as_view(&self) -> [< $t Cow>]<'_> {
53 [< $t Cow>]::Owned(owned) => [< $t Cow>]::Borrowed((*owned).as_view()),
54 [< $t Cow>]::Borrowed(borrowed) => [< $t Cow>]::Borrowed(borrowed),
59 impl<'msg> IntoView<'msg> for [< $t Cow>]<'msg> {
60 fn into_view<'shorter>(self) -> [< $t Cow>]<'shorter>
64 [< $t Cow>]::Owned(owned) => [< $t Cow>]::Owned(owned),
65 [< $t Cow>]::Borrowed(borrow) => [< $t Cow>]::Borrowed(borrow.into_view()),
70 impl IntoProxied<$t> for [< $t Cow>]<'_> {
71 fn into_proxied(self, _private: Private) -> $t {
73 [< $t Cow>]::Owned(owned) => owned,
74 [< $t Cow>]::Borrowed(borrowed) => borrowed.into_proxied(Private),
79 impl<'a> Deref for [< $t Cow>]<'a> {
82 fn deref(&self) -> View<'_, $t> {
84 [< $t Cow>]::Borrowed(borrow) => borrow,
85 [< $t Cow>]::Owned(owned) => (*owned).as_view(),
90 impl AsRef<[u8]> for [< $t Cow>]<'_> {
91 fn as_ref(&self) -> &[u8] {
93 [< $t Cow>]::Borrowed(borrow) => borrow.as_ref(),
94 [< $t Cow>]::Owned(owned) => owned.as_ref(),
104 ProtoString, ProtoStr;
108 macro_rules! impl_eq {
109 ($($t1:ty, $t2:ty);*) => {
111 impl PartialEq<$t1> for $t2 {
112 fn eq(&self, rhs: &$t1) -> bool {
113 AsRef::<[u8]>::as_ref(self) == AsRef::<[u8]>::as_ref(rhs)
122 ProtoStringCow<'_>, ProtoStringCow<'_>;
123 str, ProtoStringCow<'_>;
124 ProtoStringCow<'_>, str;
125 ProtoBytesCow<'_>, ProtoBytesCow<'_>;
126 [u8], ProtoBytesCow<'_>;
127 ProtoBytesCow<'_>, [u8]