@@ -5,9 +5,19 @@ use bytes::{Buf, BufMut};
5
5
6
6
use core:: fmt:: Debug ;
7
7
8
+ /// Represents the value of an open enum field.
9
+ ///
10
+ /// The [Protocol Buffers guide][proto-guide] specifies that unknown values
11
+ /// of fields with open enum types should be stored directly in the field
12
+ /// when decoding messages. This type provides an ergonomic way to represent
13
+ /// such values in Rust.
14
+ ///
15
+ /// [proto-guide]: https://protobuf.dev/programming-guides/enum/#definitions
8
16
#[ derive( Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
9
17
pub enum OpenEnum < T > {
18
+ /// A known value of the generated enum type.
10
19
Known ( T ) ,
20
+ /// An unknown value as decoded from the message.
11
21
Unknown ( i32 ) ,
12
22
}
13
23
@@ -27,6 +37,9 @@ impl<T> From<T> for OpenEnum<T> {
27
37
}
28
38
29
39
impl < T > OpenEnum < T > {
40
+ /// Converts a raw integer value into an open enum value.
41
+ ///
42
+ /// This method is used to decode field values from the wire format.
30
43
pub fn from_raw ( value : i32 ) -> Self
31
44
where
32
45
i32 : TryInto < T > ,
@@ -37,6 +50,7 @@ impl<T> OpenEnum<T> {
37
50
}
38
51
}
39
52
53
+ /// Converts an open enum value into its raw integer representation.
40
54
pub fn into_raw ( self ) -> i32
41
55
where
42
56
T : Into < i32 > ,
@@ -47,6 +61,9 @@ impl<T> OpenEnum<T> {
47
61
}
48
62
}
49
63
64
+ /// Converts an open enum value into its raw integer representation.
65
+ ///
66
+ /// This is a convenience method for borrowed values.
50
67
pub fn to_raw ( & self ) -> i32
51
68
where
52
69
T : Clone + Into < i32 > ,
@@ -59,20 +76,36 @@ impl<T> OpenEnum<T> {
59
76
}
60
77
61
78
impl < T > OpenEnum < T > {
79
+ /// Returns the known value of the open enum.
80
+ ///
81
+ /// # Panics
82
+ ///
83
+ /// Panics if the value is in fact unknown.
62
84
pub fn unwrap ( self ) -> T {
63
85
match self {
64
86
Self :: Known ( v) => v,
65
87
Self :: Unknown ( v) => panic ! ( "unknown field value {}" , v) ,
66
88
}
67
89
}
68
90
91
+ /// Returns the known value of the open enum, or, if the value is unknown,
92
+ /// returns the provided default value.
93
+ ///
94
+ /// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
95
+ /// the result of a function call, it is recommended to use
96
+ /// [`unwrap_or_else`] and pass a lazily evaluated closure to it.
97
+ ///
98
+ /// [`unwrap_or_else`]: #method.unwrap_or_else
69
99
pub fn unwrap_or ( self , default : T ) -> T {
70
100
match self {
71
101
Self :: Known ( v) => v,
72
102
Self :: Unknown ( _) => default,
73
103
}
74
104
}
75
105
106
+ /// Returns the known value of the open enum, or, if the value is unknown,
107
+ /// returns the value computed from the field's raw integer value by the
108
+ /// provided closure.
76
109
pub fn unwrap_or_else < F > ( self , f : F ) -> T
77
110
where
78
111
F : FnOnce ( i32 ) -> T ,
@@ -83,6 +116,8 @@ impl<T> OpenEnum<T> {
83
116
}
84
117
}
85
118
119
+ /// Returns the known value of the open enum, or, if the value is unknown,
120
+ /// returns the default value of the enum type.
86
121
pub fn unwrap_or_default ( self ) -> T
87
122
where
88
123
T : Default ,
@@ -93,20 +128,33 @@ impl<T> OpenEnum<T> {
93
128
}
94
129
}
95
130
131
+ /// If the value of the open enum is known, returns it in `Some`, otherwise
132
+ /// returns `None`.
96
133
pub fn known ( self ) -> Option < T > {
97
134
match self {
98
135
Self :: Known ( v) => Some ( v) ,
99
136
Self :: Unknown ( _) => None ,
100
137
}
101
138
}
102
139
140
+ /// If the value of the open enum is known, returns it in `Ok`, otherwise
141
+ /// returns the provided error value in `Err`.
142
+ ///
143
+ /// Arguments passed to `known_or` are eagerly evaluated; if you are passing
144
+ /// the result of a function call, it is recommended to use
145
+ /// [`known_or_else`] and pass a lazily evaluated closure to it.
146
+ ///
147
+ /// [`known_or_else`]: #method.known_or_else
103
148
pub fn known_or < E > ( self , err : E ) -> Result < T , E > {
104
149
match self {
105
150
Self :: Known ( v) => Ok ( v) ,
106
151
Self :: Unknown ( _) => Err ( err) ,
107
152
}
108
153
}
109
154
155
+ /// If the value of the open enum is known, returns it in `Ok`, otherwise
156
+ /// returns `Err` with the value computed from the field's raw integer value
157
+ /// by the provided closure.
110
158
pub fn known_or_else < E , F > ( self , err : F ) -> Result < T , E >
111
159
where
112
160
F : FnOnce ( i32 ) -> E ,
0 commit comments