blob: 94972e34f0f9a3c61e8313111b8a68ea3cd6dd42 [file] [log] [blame]
Protobuf Team Botea241fe2024-07-16 11:25:04 -07001// 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 Botc7d46972024-07-30 05:39:53 -070010use crate::__internal::SealedInternal;
Protobuf Team Bote6304eb2024-07-24 12:28:32 -070011use crate::{MutProxied, MutProxy, ViewProxy};
Protobuf Team Bot6d258462024-07-22 09:39:02 -070012use create::Parse;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -070013use interop::{MessageMutInterop, MessageViewInterop, OwnedMessageInterop};
Protobuf Team Bot6d258462024-07-22 09:39:02 -070014use read::Serialize;
Protobuf Team Botea241fe2024-07-16 11:25:04 -070015use std::fmt::Debug;
Protobuf Team Bot0f02d842024-08-05 13:27:58 -070016use write::{Clear, ClearAndParse, MergeFrom};
Protobuf Team Botea241fe2024-07-16 11:25:04 -070017
Protobuf Team Bot6d258462024-07-22 09:39:02 -070018/// A trait that all generated owned message types implement.
Protobuf Team Botc7d46972024-07-30 05:39:53 -070019pub trait Message: SealedInternal
20 + MutProxied
Protobuf Team Bot6d258462024-07-22 09:39:02 -070021 // Create traits:
Protobuf Team Bote6304eb2024-07-24 12:28:32 -070022 + Parse + Default
Protobuf Team Bot6d258462024-07-22 09:39:02 -070023 // Read traits:
Protobuf Team Bote6304eb2024-07-24 12:28:32 -070024 + Debug + Serialize
Protobuf Team Bot6d258462024-07-22 09:39:02 -070025 // Write traits:
Protobuf Team Bot0f02d842024-08-05 13:27:58 -070026 + Clear + ClearAndParse + MergeFrom
Protobuf Team Bot6d258462024-07-22 09:39:02 -070027 // Thread safety:
28 + Send + Sync
29 // Copy/Clone:
30 + Clone
Protobuf Team Bot76c767f2024-07-30 09:17:24 -070031 // C++ Interop:
32 + OwnedMessageInterop
33{
34}
Protobuf Team Botea241fe2024-07-16 11:25:04 -070035
Protobuf Team Bot6d258462024-07-22 09:39:02 -070036/// A trait that all generated message views implement.
Protobuf Team Botc7d46972024-07-30 05:39:53 -070037pub trait MessageView<'msg>: SealedInternal
38 + ViewProxy<'msg, Proxied = Self::Message>
Protobuf Team Bot6d258462024-07-22 09:39:02 -070039 // Read traits:
Protobuf Team Bote6304eb2024-07-24 12:28:32 -070040 + Debug + Serialize
Protobuf Team Bot6d258462024-07-22 09:39:02 -070041 // Thread safety:
42 + Send + Sync
Protobuf Team Bote618b282024-07-24 08:58:57 -070043 // Copy/Clone:
44 + Copy + Clone
Protobuf Team Bot76c767f2024-07-30 09:17:24 -070045 // C++ Interop:
46 + MessageViewInterop<'msg>
Protobuf Team Bote6304eb2024-07-24 12:28:32 -070047{
48 #[doc(hidden)]
49 type Message: Message;
50}
Protobuf Team Botea241fe2024-07-16 11:25:04 -070051
Protobuf Team Bot6d258462024-07-22 09:39:02 -070052/// A trait that all generated message muts implement.
Protobuf Team Botc7d46972024-07-30 05:39:53 -070053pub trait MessageMut<'msg>: SealedInternal
54 + MutProxy<'msg, MutProxied = Self::Message>
Protobuf Team Bot6d258462024-07-22 09:39:02 -070055 // Read traits:
Protobuf Team Bote6304eb2024-07-24 12:28:32 -070056 + Debug + Serialize
Protobuf Team Bot6d258462024-07-22 09:39:02 -070057 // Write traits:
Protobuf Team Bot0fbba322024-07-26 10:39:35 -070058 // TODO: MsgMut should impl ClearAndParse.
Protobuf Team Bot0f02d842024-08-05 13:27:58 -070059 + Clear + MergeFrom
Protobuf Team Bot6d258462024-07-22 09:39:02 -070060 // Thread safety:
61 + Sync
Protobuf Team Bot0fbba322024-07-26 10:39:35 -070062 // Copy/Clone:
63 // (Neither)
Protobuf Team Bot76c767f2024-07-30 09:17:24 -070064 // C++ Interop:
65 + MessageMutInterop<'msg>
Protobuf Team Bot6d258462024-07-22 09:39:02 -070066{
67 #[doc(hidden)]
68 type Message: Message;
69}
70
71/// Operations related to constructing a message. Only owned messages implement
72/// these traits.
73pub(crate) mod create {
Protobuf Team Botc7d46972024-07-30 05:39:53 -070074 use super::SealedInternal;
75 pub trait Parse: SealedInternal + Sized {
Protobuf Team Bot6d258462024-07-22 09:39:02 -070076 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.
83pub(crate) mod read {
Protobuf Team Botc7d46972024-07-30 05:39:53 -070084 use super::SealedInternal;
85
86 pub trait Serialize: SealedInternal {
Protobuf Team Bot6d258462024-07-22 09:39:02 -070087 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.
94pub(crate) mod write {
Protobuf Team Botc7d46972024-07-30 05:39:53 -070095 use super::SealedInternal;
Protobuf Team Bot0f02d842024-08-05 13:27:58 -070096 use crate::AsView;
Protobuf Team Bot6d258462024-07-22 09:39:02 -070097
Protobuf Team Botc7d46972024-07-30 05:39:53 -070098 pub trait Clear: SealedInternal {
Protobuf Team Bot6d258462024-07-22 09:39:02 -070099 fn clear(&mut self);
100 }
101
Protobuf Team Botc7d46972024-07-30 05:39:53 -0700102 pub trait ClearAndParse: SealedInternal {
Protobuf Team Bot6d258462024-07-22 09:39:02 -0700103 fn clear_and_parse(&mut self, data: &[u8]) -> Result<(), crate::ParseError>;
104 }
Protobuf Team Bot0f02d842024-08-05 13:27:58 -0700105
106 pub trait MergeFrom: AsView + SealedInternal {
107 fn merge_from(&mut self, src: impl AsView<Proxied = Self::Proxied>);
108 }
Protobuf Team Bot6d258462024-07-22 09:39:02 -0700109}
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700110
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.
115pub(crate) mod interop {
Protobuf Team Botb0db5bd2024-08-20 06:23:29 -0700116 use super::SealedInternal;
117 use std::ffi::c_void;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700118
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 Botb0db5bd2024-08-20 06:23:29 -0700123 /// Drops `self` and returns an underlying pointer that it was wrapping
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700124 /// without deleting it.
125 ///
Protobuf Team Botb0db5bd2024-08-20 06:23:29 -0700126 /// The caller is responsible for ensuring the returned pointer is
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700127 /// 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 Botb0db5bd2024-08-20 06:23:29 -0700130 fn __unstable_leak_raw_message(self) -> *mut c_void;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700131
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 Botb0db5bd2024-08-20 06:23:29 -0700139 unsafe fn __unstable_take_ownership_of_raw_message(raw_message: *mut c_void) -> Self;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700140 }
141
142 /// Traits related to message view interop.
143 pub trait MessageViewInterop<'msg>: SealedInternal {
Protobuf Team Botb0db5bd2024-08-20 06:23:29 -0700144 /// Borrows `self` as an underlying C++ raw pointer.
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700145 ///
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 Botb0db5bd2024-08-20 06:23:29 -0700151 fn __unstable_as_raw_message(&self) -> *const c_void;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700152
Protobuf Team Botb0db5bd2024-08-20 06:23:29 -0700153 /// 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 Bot76c767f2024-07-30 09:17:24 -0700156 ///
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 Botb0db5bd2024-08-20 06:23:29 -0700161 unsafe fn __unstable_wrap_raw_message(raw: &'msg *const c_void) -> Self;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700162 }
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 Botb0db5bd2024-08-20 06:23:29 -0700168 /// Exclusive borrows `self` as an underlying mutable C++ raw pointer.
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700169 ///
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 Botb0db5bd2024-08-20 06:23:29 -0700175 fn __unstable_as_raw_message_mut(&mut self) -> *mut c_void;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700176
Protobuf Team Botb0db5bd2024-08-20 06:23:29 -0700177 /// Wraps the provided C++ pointer as a MessageMut.
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700178 ///
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 Botb0db5bd2024-08-20 06:23:29 -0700184 unsafe fn __unstable_wrap_raw_message_mut(raw: &'msg mut *mut c_void) -> Self;
Protobuf Team Bot76c767f2024-07-30 09:17:24 -0700185 }
186}