Protobuf Team Bot | ea241fe | 2024-07-16 11:25:04 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2024 Google LLC. All rights reserved. |
| 3 | // |
| 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 |
| 7 | |
| 8 | //! Traits that are implemeted by codegen types. |
| 9 | |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 10 | use crate::__internal::SealedInternal; |
Protobuf Team Bot | e6304eb | 2024-07-24 12:28:32 -0700 | [diff] [blame] | 11 | use crate::{MutProxied, MutProxy, ViewProxy}; |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 12 | use create::Parse; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 13 | use interop::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop}; |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 14 | use read::Serialize; |
Protobuf Team Bot | ea241fe | 2024-07-16 11:25:04 -0700 | [diff] [blame] | 15 | use std::fmt::Debug; |
Protobuf Team Bot | 0f02d84 | 2024-08-05 13:27:58 -0700 | [diff] [blame] | 16 | use write::{Clear, ClearAndParse, MergeFrom}; |
Protobuf Team Bot | ea241fe | 2024-07-16 11:25:04 -0700 | [diff] [blame] | 17 | |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 18 | /// A trait that all generated owned message types implement. |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 19 | pub trait Message: SealedInternal |
| 20 | + MutProxied |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 21 | // Create traits: |
Protobuf Team Bot | e6304eb | 2024-07-24 12:28:32 -0700 | [diff] [blame] | 22 | + Parse + Default |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 23 | // Read traits: |
Protobuf Team Bot | e6304eb | 2024-07-24 12:28:32 -0700 | [diff] [blame] | 24 | + Debug + Serialize |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 25 | // Write traits: |
Protobuf Team Bot | 0f02d84 | 2024-08-05 13:27:58 -0700 | [diff] [blame] | 26 | + Clear + ClearAndParse + MergeFrom |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 27 | // Thread safety: |
| 28 | + Send + Sync |
| 29 | // Copy/Clone: |
| 30 | + Clone |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 31 | // C++ Interop: |
| 32 | + OwnedMessageInterop |
| 33 | { |
| 34 | } |
Protobuf Team Bot | ea241fe | 2024-07-16 11:25:04 -0700 | [diff] [blame] | 35 | |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 36 | /// A trait that all generated message views implement. |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 37 | pub trait MessageView<'msg>: SealedInternal |
| 38 | + ViewProxy<'msg, Proxied = Self::Message> |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 39 | // Read traits: |
Protobuf Team Bot | e6304eb | 2024-07-24 12:28:32 -0700 | [diff] [blame] | 40 | + Debug + Serialize |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 41 | // Thread safety: |
| 42 | + Send + Sync |
Protobuf Team Bot | e618b28 | 2024-07-24 08:58:57 -0700 | [diff] [blame] | 43 | // Copy/Clone: |
| 44 | + Copy + Clone |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 45 | // C++ Interop: |
| 46 | + MessageViewInterop<'msg> |
Protobuf Team Bot | e6304eb | 2024-07-24 12:28:32 -0700 | [diff] [blame] | 47 | { |
| 48 | #[doc(hidden)] |
| 49 | type Message: Message; |
| 50 | } |
Protobuf Team Bot | ea241fe | 2024-07-16 11:25:04 -0700 | [diff] [blame] | 51 | |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 52 | /// A trait that all generated message muts implement. |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 53 | pub trait MessageMut<'msg>: SealedInternal |
| 54 | + MutProxy<'msg, MutProxied = Self::Message> |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 55 | // Read traits: |
Protobuf Team Bot | e6304eb | 2024-07-24 12:28:32 -0700 | [diff] [blame] | 56 | + Debug + Serialize |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 57 | // Write traits: |
Protobuf Team Bot | 0fbba32 | 2024-07-26 10:39:35 -0700 | [diff] [blame] | 58 | // TODO: MsgMut should impl ClearAndParse. |
Protobuf Team Bot | 0f02d84 | 2024-08-05 13:27:58 -0700 | [diff] [blame] | 59 | + Clear + MergeFrom |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 60 | // Thread safety: |
| 61 | + Sync |
Protobuf Team Bot | 0fbba32 | 2024-07-26 10:39:35 -0700 | [diff] [blame] | 62 | // Copy/Clone: |
| 63 | // (Neither) |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 64 | // C++ Interop: |
| 65 | + MessageMutInterop<'msg> |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 66 | { |
| 67 | #[doc(hidden)] |
| 68 | type Message: Message; |
| 69 | } |
| 70 | |
| 71 | /// Operations related to constructing a message. Only owned messages implement |
| 72 | /// these traits. |
| 73 | pub(crate) mod create { |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 74 | use super::SealedInternal; |
| 75 | pub trait Parse: SealedInternal + Sized { |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 76 | fn parse(serialized: &[u8]) -> Result<Self, crate::ParseError>; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// Operations related to reading some aspect of a message (methods that would |
| 81 | /// have a `&self` receiver on an owned message). Owned messages, views, and |
| 82 | /// muts all implement these traits. |
| 83 | pub(crate) mod read { |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 84 | use super::SealedInternal; |
| 85 | |
| 86 | pub trait Serialize: SealedInternal { |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 87 | fn serialize(&self) -> Result<Vec<u8>, crate::SerializeError>; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Operations related to mutating a message (methods that would have a `&mut |
| 92 | /// self` receiver on an owned message). Owned messages and muts implement these |
| 93 | /// traits. |
| 94 | pub(crate) mod write { |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 95 | use super::SealedInternal; |
Protobuf Team Bot | 0f02d84 | 2024-08-05 13:27:58 -0700 | [diff] [blame] | 96 | use crate::AsView; |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 97 | |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 98 | pub trait Clear: SealedInternal { |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 99 | fn clear(&mut self); |
| 100 | } |
| 101 | |
Protobuf Team Bot | c7d4697 | 2024-07-30 05:39:53 -0700 | [diff] [blame] | 102 | pub trait ClearAndParse: SealedInternal { |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 103 | fn clear_and_parse(&mut self, data: &[u8]) -> Result<(), crate::ParseError>; |
| 104 | } |
Protobuf Team Bot | 0f02d84 | 2024-08-05 13:27:58 -0700 | [diff] [blame] | 105 | |
| 106 | pub trait MergeFrom: AsView + SealedInternal { |
| 107 | fn merge_from(&mut self, src: impl AsView<Proxied = Self::Proxied>); |
| 108 | } |
Protobuf Team Bot | 6d25846 | 2024-07-22 09:39:02 -0700 | [diff] [blame] | 109 | } |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 110 | |
| 111 | /// Traits related to interop with C or C++. |
| 112 | /// |
| 113 | /// These traits are deliberately not available on the prelude, as they should |
| 114 | /// be used rarely and with great care. |
| 115 | pub(crate) mod interop { |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 116 | use super::SealedInternal; |
| 117 | use std::ffi::c_void; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 118 | |
| 119 | /// Traits related to owned message interop. Note that these trait fns |
| 120 | /// are only available on C++ kernel as upb interop of owned messages |
| 121 | /// requires more work to handle the Arena behavior. |
| 122 | pub trait OwnedMessageInterop: SealedInternal { |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 123 | /// Drops `self` and returns an underlying pointer that it was wrapping |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 124 | /// without deleting it. |
| 125 | /// |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 126 | /// The caller is responsible for ensuring the returned pointer is |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 127 | /// subsequently deleted (eg by moving it into a std::unique_ptr in |
| 128 | /// C++), or else it will leak. |
| 129 | #[cfg(cpp_kernel)] |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 130 | fn __unstable_leak_raw_message(self) -> *mut c_void; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 131 | |
| 132 | /// Takes exclusive ownership of the `raw_message`. |
| 133 | /// |
| 134 | /// # Safety |
| 135 | /// - The underlying message must be for the same type as `Self` |
| 136 | /// - The pointer passed in must not be used by the caller after being |
| 137 | /// passed here (must not be read, written, or deleted) |
| 138 | #[cfg(cpp_kernel)] |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 139 | unsafe fn __unstable_take_ownership_of_raw_message(raw_message: *mut c_void) -> Self; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /// Traits related to message view interop. |
| 143 | pub trait MessageViewInterop<'msg>: SealedInternal { |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 144 | /// Borrows `self` as an underlying C++ raw pointer. |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 145 | /// |
| 146 | /// Note that the returned Value must be used under the same constraints |
| 147 | /// as though it were a borrow of `self`: it should be treated as a |
| 148 | /// `const Message*` in C++, and not be mutated in any way, and any |
| 149 | /// mutation to the parent message may invalidate it, and it |
| 150 | /// must not be deleted. |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 151 | fn __unstable_as_raw_message(&self) -> *const c_void; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 152 | |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 153 | /// Wraps the provided pointer as a MessageView. This takes a ref |
| 154 | /// of a pointer so that a stack variable's lifetime can be used |
| 155 | /// to help make the borrow checker safer. |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 156 | /// |
| 157 | /// # Safety |
| 158 | /// - The underlying message must be for the same type as `Self` |
| 159 | /// - The underlying message must be alive for 'msg and not mutated |
| 160 | /// while the wrapper is live. |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 161 | unsafe fn __unstable_wrap_raw_message(raw: &'msg *const c_void) -> Self; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /// Traits related to message mut interop. Note that these trait fns |
| 165 | /// are only available on C++ kernel as upb interop of owned messages |
| 166 | /// requires more work to handle the Arena behavior. |
| 167 | pub trait MessageMutInterop<'msg>: SealedInternal { |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 168 | /// Exclusive borrows `self` as an underlying mutable C++ raw pointer. |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 169 | /// |
| 170 | /// Note that the returned Value must be used under the same constraints |
| 171 | /// as though it were a mut borrow of `self`: it should be treated as a |
| 172 | /// non-owned `Message*` in C++. And any mutation to the parent message |
| 173 | /// may invalidate it, and it must not be deleted. |
| 174 | #[cfg(cpp_kernel)] |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 175 | fn __unstable_as_raw_message_mut(&mut self) -> *mut c_void; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 176 | |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 177 | /// Wraps the provided C++ pointer as a MessageMut. |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 178 | /// |
| 179 | /// # Safety |
| 180 | /// - The underlying message must be for the same type as `Self` |
| 181 | /// - The underlying message must be alive for 'msg and not read or |
| 182 | /// mutated while the wrapper is live. |
| 183 | #[cfg(cpp_kernel)] |
Protobuf Team Bot | b0db5bd | 2024-08-20 06:23:29 -0700 | [diff] [blame] | 184 | unsafe fn __unstable_wrap_raw_message_mut(raw: &'msg mut *mut c_void) -> Self; |
Protobuf Team Bot | 76c767f | 2024-07-30 09:17:24 -0700 | [diff] [blame] | 185 | } |
| 186 | } |