protobuf/
misc.rs

1use std::mem;
2use std::mem::MaybeUninit;
3
4use crate::well_known_types;
5
6/// `MaybeUninit::write_slice` is not stable.
7pub(crate) fn maybe_uninit_write_slice<'a, T>(
8    this: &'a mut [MaybeUninit<T>],
9    src: &[T],
10) -> &'a mut [T]
11where
12    T: Copy,
13{
14    // SAFETY: copy-paste from rust stdlib.
15
16    let uninit_src: &[MaybeUninit<T>] = unsafe { mem::transmute(src) };
17
18    this.copy_from_slice(uninit_src);
19
20    unsafe { &mut *(this as *mut [MaybeUninit<T>] as *mut [T]) }
21}
22
23/// `MaybeUninit::array_assume_init` is not stable.
24#[inline]
25pub(crate) unsafe fn maybe_ununit_array_assume_init<T, const N: usize>(
26    array: [MaybeUninit<T>; N],
27) -> [T; N] {
28    // SAFETY:
29    // * The caller guarantees that all elements of the array are initialized
30    // * `MaybeUninit<T>` and T are guaranteed to have the same layout
31    // * `MaybeUninit` does not drop, so there are no double-frees
32    // And thus the conversion is safe
33    (&array as *const _ as *const [T; N]).read()
34}
35
36// bool <-> BoolValue
37
38impl From<well_known_types::wrappers::BoolValue> for bool {
39    fn from(inner: well_known_types::wrappers::BoolValue) -> Self {
40        inner.value
41    }
42}
43
44impl From<bool> for well_known_types::wrappers::BoolValue {
45    fn from(inner: bool) -> Self {
46        let mut value = Self::new();
47        value.value = inner;
48        value
49    }
50}
51
52// Vec<u8> <-> BytesValue
53
54impl From<well_known_types::wrappers::BytesValue> for Vec<u8> {
55    fn from(inner: well_known_types::wrappers::BytesValue) -> Self {
56        inner.value
57    }
58}
59
60impl From<Vec<u8>> for well_known_types::wrappers::BytesValue {
61    fn from(inner: Vec<u8>) -> Self {
62        let mut value = Self::new();
63        value.value = inner;
64        value
65    }
66}
67
68// f64 <-> DoubleValue
69
70impl From<well_known_types::wrappers::DoubleValue> for f64 {
71    fn from(inner: well_known_types::wrappers::DoubleValue) -> Self {
72        inner.value
73    }
74}
75
76impl From<f64> for well_known_types::wrappers::DoubleValue {
77    fn from(inner: f64) -> Self {
78        let mut value = Self::new();
79        value.value = inner;
80        value
81    }
82}
83
84// f32 <-> FloatValue
85
86impl From<well_known_types::wrappers::FloatValue> for f32 {
87    fn from(inner: well_known_types::wrappers::FloatValue) -> Self {
88        inner.value
89    }
90}
91
92impl From<f32> for well_known_types::wrappers::FloatValue {
93    fn from(inner: f32) -> Self {
94        let mut value = Self::new();
95        value.value = inner;
96        value
97    }
98}
99
100// i32 <-> Int32Value
101
102impl From<well_known_types::wrappers::Int32Value> for i32 {
103    fn from(inner: well_known_types::wrappers::Int32Value) -> Self {
104        inner.value
105    }
106}
107
108impl From<i32> for well_known_types::wrappers::Int32Value {
109    fn from(inner: i32) -> Self {
110        let mut value = Self::new();
111        value.value = inner;
112        value
113    }
114}
115
116// i64 <-> Int64Value
117
118impl From<well_known_types::wrappers::Int64Value> for i64 {
119    fn from(inner: well_known_types::wrappers::Int64Value) -> Self {
120        inner.value
121    }
122}
123
124impl From<i64> for well_known_types::wrappers::Int64Value {
125    fn from(inner: i64) -> Self {
126        let mut value = Self::new();
127        value.value = inner;
128        value
129    }
130}
131
132// u32 <-> UInt32Value
133
134impl From<well_known_types::wrappers::UInt32Value> for u32 {
135    fn from(inner: well_known_types::wrappers::UInt32Value) -> Self {
136        inner.value
137    }
138}
139
140impl From<u32> for well_known_types::wrappers::UInt32Value {
141    fn from(inner: u32) -> Self {
142        let mut value = Self::new();
143        value.value = inner;
144        value
145    }
146}
147
148// u64 <-> UInt64Value
149
150impl From<well_known_types::wrappers::UInt64Value> for u64 {
151    fn from(inner: well_known_types::wrappers::UInt64Value) -> Self {
152        inner.value
153    }
154}
155
156impl From<u64> for well_known_types::wrappers::UInt64Value {
157    fn from(inner: u64) -> Self {
158        let mut value = Self::new();
159        value.value = inner;
160        value
161    }
162}
163
164// () <-> Empty
165
166impl From<well_known_types::empty::Empty> for () {
167    fn from(_inner: well_known_types::empty::Empty) -> Self {}
168}
169
170impl From<()> for well_known_types::empty::Empty {
171    fn from(_inner: ()) -> Self {
172        Self::new()
173    }
174}