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