-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathmisc.rs
178 lines (145 loc) · 3.78 KB
/
misc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::mem;
use crate::well_known_types;
/// Slice from `vec[vec.len()..vec.capacity()]`
pub unsafe fn remaining_capacity_as_slice_mut<A>(vec: &mut Vec<A>) -> &mut [A] {
let range = vec.len()..vec.capacity();
vec.get_unchecked_mut(range)
}
pub unsafe fn remove_lifetime_mut<A: ?Sized>(a: &mut A) -> &'static mut A {
mem::transmute(a)
}
// bool <-> BoolValue
impl From<well_known_types::BoolValue> for bool {
fn from(inner: well_known_types::BoolValue) -> Self {
inner.value
}
}
impl From<bool> for well_known_types::BoolValue {
fn from(inner: bool) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// Vec<u8> <-> BytesValue
impl From<well_known_types::BytesValue> for Vec<u8> {
fn from(inner: well_known_types::BytesValue) -> Self {
inner.value
}
}
impl From<Vec<u8>> for well_known_types::BytesValue {
fn from(inner: Vec<u8>) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// f64 <-> DoubleValue
impl From<well_known_types::DoubleValue> for f64 {
fn from(inner: well_known_types::DoubleValue) -> Self {
inner.value
}
}
impl From<f64> for well_known_types::DoubleValue {
fn from(inner: f64) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// f32 <-> FloatValue
impl From<well_known_types::FloatValue> for f32 {
fn from(inner: well_known_types::FloatValue) -> Self {
inner.value
}
}
impl From<f32> for well_known_types::FloatValue {
fn from(inner: f32) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// i32 <-> Int32Value
impl From<well_known_types::Int32Value> for i32 {
fn from(inner: well_known_types::Int32Value) -> Self {
inner.value
}
}
impl From<i32> for well_known_types::Int32Value {
fn from(inner: i32) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// i64 <-> Int64Value
impl From<well_known_types::Int64Value> for i64 {
fn from(inner: well_known_types::Int64Value) -> Self {
inner.value
}
}
impl From<i64> for well_known_types::Int64Value {
fn from(inner: i64) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// u32 <-> UInt32Value
impl From<well_known_types::UInt32Value> for u32 {
fn from(inner: well_known_types::UInt32Value) -> Self {
inner.value
}
}
impl From<u32> for well_known_types::UInt32Value {
fn from(inner: u32) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// u64 <-> UInt64Value
impl From<well_known_types::UInt64Value> for u64 {
fn from(inner: well_known_types::UInt64Value) -> Self {
inner.value
}
}
impl From<u64> for well_known_types::UInt64Value {
fn from(inner: u64) -> Self {
let mut value = Self::new();
value.value = inner;
value
}
}
// () <-> Empty
impl From<well_known_types::Empty> for () {
fn from(_inner: well_known_types::Empty) -> Self {}
}
impl From<()> for well_known_types::Empty {
fn from(_inner: ()) -> Self {
Self::new()
}
}
// TODO Think about `std::time::Duration` and `std::time::SystemTime` conversions.
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_remaining_capacity_as_slice_mut() {
let mut v = Vec::with_capacity(5);
v.push(10);
v.push(11);
v.push(12);
unsafe {
{
let s = remaining_capacity_as_slice_mut(&mut v);
assert_eq!(2, s.len());
s[0] = 13;
s[1] = 14;
}
v.set_len(5);
}
assert_eq!(vec![10, 11, 12, 13, 14], v);
}
}