File: cord.rs

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 3,696 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

use crate::__internal::{Private, SealedInternal};
use crate::{
    AsView, IntoProxied, IntoView, ProtoBytes, ProtoStr, ProtoString, Proxied, Proxy, View,
    ViewProxy,
};
use paste::paste;
use std::cmp::PartialEq;
use std::ops::Deref;

macro_rules! impl_cord_types {
    ($($t:ty, $vt:ty);*) => {
        paste! { $(
          #[derive(Debug)]
          pub struct [< $t Cord>](Private);

          #[derive(Debug)]
          pub enum [< $t Cow>]<'a> {
            Borrowed(View<'a, $t>),
            Owned($t),
          }

          impl SealedInternal for [< $t Cord>] {}

          impl<'msg> SealedInternal for [< $t Cow>]<'msg> {}

          impl Proxied for [< $t Cord>] {
            type View<'msg> = [< $t Cow>]<'msg>;
          }

          impl AsView for [< $t Cord>] {
            type Proxied = Self;
            fn as_view(&self) -> [< $t Cow>]<'_> {
              unimplemented!("Proto Cord should never be constructed");
            }
          }

          impl<'msg> Proxy<'msg> for [< $t Cow>]<'msg> {}

          impl<'msg> ViewProxy<'msg> for [< $t Cow>]<'msg> {}

          impl<'msg> AsView for [< $t Cow>]<'msg> {
            type Proxied = [< $t Cord>];

            fn as_view(&self) -> [< $t Cow>]<'_> {
              match self {
                [< $t Cow>]::Owned(owned) => [< $t Cow>]::Borrowed((*owned).as_view()),
                [< $t Cow>]::Borrowed(borrowed) => [< $t Cow>]::Borrowed(borrowed),
              }
            }
          }

          impl<'msg> IntoView<'msg> for [< $t Cow>]<'msg> {
            fn into_view<'shorter>(self) -> [< $t Cow>]<'shorter>
            where
                'msg: 'shorter, {
              match self {
                [< $t Cow>]::Owned(owned) => [< $t Cow>]::Owned(owned),
                [< $t Cow>]::Borrowed(borrow) => [< $t Cow>]::Borrowed(borrow.into_view()),
              }
            }
          }

          impl IntoProxied<$t> for [< $t Cow>]<'_> {
              fn into_proxied(self, _private: Private) -> $t {
                match self {
                  [< $t Cow>]::Owned(owned) => owned,
                  [< $t Cow>]::Borrowed(borrowed) => borrowed.into_proxied(Private),
                }
              }
          }

          impl<'a> Deref for [< $t Cow>]<'a> {
            type Target = $vt;

            fn deref(&self) -> View<'_, $t> {
                match self {
                    [< $t Cow>]::Borrowed(borrow) => borrow,
                    [< $t Cow>]::Owned(owned) => (*owned).as_view(),
                }
            }
          }

          impl AsRef<[u8]> for [< $t Cow>]<'_> {
            fn as_ref(&self) -> &[u8] {
                match self {
                  [< $t Cow>]::Borrowed(borrow) => borrow.as_ref(),
                  [< $t Cow>]::Owned(owned) => owned.as_ref(),
                }
            }
          }
          )*
        }
    }
}

impl_cord_types!(
    ProtoString, ProtoStr;
    ProtoBytes, [u8]
);

macro_rules! impl_eq {
  ($($t1:ty, $t2:ty);*) => {
      paste! { $(
        impl PartialEq<$t1> for $t2 {
          fn eq(&self, rhs: &$t1) -> bool {
              AsRef::<[u8]>::as_ref(self) == AsRef::<[u8]>::as_ref(rhs)
          }
        }
        )*
      }
  }
}

impl_eq!(
  ProtoStringCow<'_>, ProtoStringCow<'_>;
  str, ProtoStringCow<'_>;
  ProtoStringCow<'_>, str;
  ProtoBytesCow<'_>, ProtoBytesCow<'_>;
  [u8], ProtoBytesCow<'_>;
  ProtoBytesCow<'_>, [u8]
);