/rust/registry/src/index.crates.io-6f17d22bba15001f/protobuf-3.2.0/src/owning_ref.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Utility similar to provided by `owning_ref` crate. |
2 | | |
3 | | use std::fmt; |
4 | | use std::fmt::Debug; |
5 | | use std::ops::Deref; |
6 | | use std::sync::Arc; |
7 | | |
8 | | enum Owner<A: 'static> { |
9 | | Arc(Arc<A>), |
10 | | Static(&'static A), |
11 | | } |
12 | | |
13 | | impl<A: 'static> Deref for Owner<A> { |
14 | | type Target = A; |
15 | | |
16 | 0 | fn deref(&self) -> &A { |
17 | 0 | match self { |
18 | 0 | Owner::Arc(a) => &*a, |
19 | 0 | Owner::Static(a) => a, |
20 | | } |
21 | 0 | } |
22 | | } |
23 | | |
24 | | pub(crate) struct OwningRef<A: 'static, B: 'static> { |
25 | | owner: Owner<A>, |
26 | | ptr: *const B, |
27 | | } |
28 | | |
29 | | unsafe impl<A: Send + Sync + 'static, B: Send + Sync + 'static> Sync for OwningRef<A, B> {} |
30 | | unsafe impl<A: Send + Sync + 'static, B: Send + Sync + 'static> Send for OwningRef<A, B> {} |
31 | | |
32 | | impl<A: 'static, B: 'static> Deref for OwningRef<A, B> { |
33 | | type Target = B; |
34 | | |
35 | 0 | fn deref(&self) -> &B { |
36 | 0 | // SAFETY: `self.owner` owns the data and it is not movable. |
37 | 0 | unsafe { &*self.ptr } |
38 | 0 | } Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FieldDescriptorProto> as core::ops::deref::Deref>::deref Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FileDescriptorProto> as core::ops::deref::Deref>::deref Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto> as core::ops::deref::Deref>::deref Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::EnumDescriptorProto> as core::ops::deref::Deref>::deref |
39 | | } |
40 | | |
41 | | impl<A: 'static> Clone for Owner<A> { |
42 | 0 | fn clone(&self) -> Owner<A> { |
43 | 0 | match self { |
44 | 0 | Owner::Arc(arc) => Owner::Arc(arc.clone()), |
45 | 0 | Owner::Static(ptr) => Owner::Static(ptr), |
46 | | } |
47 | 0 | } |
48 | | } |
49 | | |
50 | | impl<A: 'static, B: 'static> Clone for OwningRef<A, B> { |
51 | 0 | fn clone(&self) -> OwningRef<A, B> { |
52 | 0 | OwningRef { |
53 | 0 | ptr: self.ptr, |
54 | 0 | owner: self.owner.clone(), |
55 | 0 | } |
56 | 0 | } |
57 | | } |
58 | | |
59 | | impl<A: 'static, B: fmt::Debug + 'static> Debug for OwningRef<A, B> { |
60 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
61 | 0 | Debug::fmt(&**self, f) |
62 | 0 | } |
63 | | } |
64 | | |
65 | | impl<A: 'static> OwningRef<A, A> { |
66 | 0 | pub(crate) fn new_arc(arc: Arc<A>) -> OwningRef<A, A> { |
67 | 0 | OwningRef { |
68 | 0 | ptr: Arc::as_ptr(&arc), |
69 | 0 | owner: Owner::Arc(arc), |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | 0 | pub(crate) fn new_static(ptr: &'static A) -> OwningRef<A, A> { |
74 | 0 | OwningRef { |
75 | 0 | ptr, |
76 | 0 | owner: Owner::Static(ptr), |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | 0 | pub(crate) fn owner(&self) -> &A { |
81 | 0 | &self.owner |
82 | 0 | } |
83 | | } |
84 | | |
85 | | impl<A: 'static, B: 'static> OwningRef<A, B> { |
86 | 0 | pub(crate) fn _map<C>(self, f: impl FnOnce(&B) -> &C) -> OwningRef<A, C> { |
87 | 0 | let ptr = f(&*self); |
88 | 0 | OwningRef { |
89 | 0 | ptr, |
90 | 0 | owner: self.owner, |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | 0 | pub(crate) fn flat_map_slice<'x, C, T: FnOnce(&B) -> &[C]>( |
95 | 0 | &self, |
96 | 0 | f: T, |
97 | 0 | ) -> impl Iterator<Item = OwningRef<A, C>> + '_ |
98 | 0 | where |
99 | 0 | C: 'static, |
100 | 0 | { |
101 | 0 | f(&self).into_iter().map(|ptr| OwningRef { |
102 | 0 | ptr, |
103 | 0 | owner: self.owner.clone(), |
104 | 0 | }) Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FileDescriptorProto>>::flat_map_slice::<protobuf::descriptor::DescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::new::{closure#1}>::{closure#0} Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FileDescriptorProto>>::flat_map_slice::<protobuf::descriptor::EnumDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::new::{closure#0}>::{closure#0} Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FileDescriptorProto>>::flat_map_slice::<protobuf::descriptor::FieldDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::new::{closure#2}>::{closure#0} Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::DescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message_and_inners::{closure#3}>::{closure#0} Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::EnumDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message_and_inners::{closure#0}>::{closure#0} Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::FieldDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message::{closure#0}>::{closure#0} Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::FieldDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message::{closure#1}>::{closure#0} |
105 | 0 | } Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FileDescriptorProto>>::flat_map_slice::<protobuf::descriptor::DescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::new::{closure#1}> Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FileDescriptorProto>>::flat_map_slice::<protobuf::descriptor::EnumDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::new::{closure#0}> Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::FileDescriptorProto>>::flat_map_slice::<protobuf::descriptor::FieldDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::new::{closure#2}> Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::DescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message_and_inners::{closure#3}> Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::EnumDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message_and_inners::{closure#0}> Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::FieldDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message::{closure#0}> Unexecuted instantiation: <protobuf::owning_ref::OwningRef<protobuf::descriptor::FileDescriptorProto, protobuf::descriptor::DescriptorProto>>::flat_map_slice::<protobuf::descriptor::FieldDescriptorProto, <protobuf::reflect::file::index::FileDescriptorCommon>::index_message::{closure#1}> |
106 | | } |