• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 use super::upb_MiniTable;
9 
10 /// A trait for types which have a constant associated MiniTable (e.g.
11 /// generated messages, and their mut and view proxy types).
12 ///
13 /// upb_Message in C is effectively a DST type, where instances are created with
14 /// a MiniTable (and have a size dependent on the given MiniTable). Many upb
15 /// operations on the upb_Message* will require the same upb_MiniTable* to be
16 /// passed in as a parameter, which is referred to as 'the associated MiniTable
17 /// for the upb_Message instance' in safety comments.
18 ///
19 /// This trait is a way to associate a MiniTable with Rust types
20 /// which hold upb_Message* to simplify ensuring the upb C invariants
21 /// are maintained.
22 ///
23 /// Note that this would prefer to be a `const MINI_TABLE: *const upb_MiniTable`
24 /// to statically associate a single MiniTable, but as long as the MiniTable is
25 /// an extern "C" we cannot do that without the unstable `const_refs_to_static`.
26 /// After that feature is stabilized (or if we move the MiniTable generation to
27 /// .rs) this will be switched.
28 ///
29 /// SAFETY:
30 /// - The MiniTable pointer must be from Protobuf code generation and follow the
31 ///   corresponding invariants associated with upb's C API (the pointer should
32 ///   always have the same non-null value, the underlying pointee should never
33 ///   be modified and should have 'static lifetime).
34 pub unsafe trait AssociatedMiniTable {
mini_table() -> *const upb_MiniTable35     fn mini_table() -> *const upb_MiniTable;
36 }
37