prost_reflect/descriptor/
global.rs

1use std::sync::{Mutex, MutexGuard, OnceLock};
2
3use prost::bytes::Buf;
4use prost_types::FileDescriptorProto;
5
6use crate::{DescriptorError, DescriptorPool};
7
8static INSTANCE: OnceLock<Mutex<DescriptorPool>> = OnceLock::new();
9
10fn instance() -> MutexGuard<'static, DescriptorPool> {
11    INSTANCE
12        .get_or_init(|| Mutex::new(crate::reflect::make_wkt_descriptor_pool().unwrap()))
13        .lock()
14        .unwrap()
15}
16
17impl DescriptorPool {
18    /// Gets a copy of the global descriptor pool. By default, this just contains the google well-known types.
19    ///
20    /// The global descriptor pool is typically used as a convenient place to store descriptors for `ReflectMessage` implementations.
21    ///
22    /// Note that modifications to the returned pool won't affect the global pool - use
23    /// [`decode_global_file_descriptor_set`](DescriptorPool::decode_global_file_descriptor_set) or
24    /// [`add_global_file_descriptor_proto`](DescriptorPool::add_global_file_descriptor_proto) to modify the global pool.
25    pub fn global() -> DescriptorPool {
26        instance().clone()
27    }
28
29    /// Decodes and adds a set of file descriptors to the global pool.
30    ///
31    /// See [`DescriptorPool::decode_file_descriptor_set`] for more details.
32    pub fn decode_global_file_descriptor_set<B>(bytes: B) -> Result<(), DescriptorError>
33    where
34        B: Buf,
35    {
36        instance().decode_file_descriptor_set(bytes)?;
37        Ok(())
38    }
39
40    /// Adds a single file descriptor to the global pool.
41    ///
42    /// See [`DescriptorPool::add_file_descriptor_proto`] for more details.
43    pub fn add_global_file_descriptor_proto(
44        file: FileDescriptorProto,
45    ) -> Result<(), DescriptorError> {
46        instance().add_file_descriptor_proto(file)?;
47        Ok(())
48    }
49}