Skip to content

Commit e4c84e7

Browse files
committed
prost: document OpenEnum
1 parent 29da76b commit e4c84e7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

prost/src/open_enum.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ use bytes::{Buf, BufMut};
55

66
use core::fmt::Debug;
77

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
816
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
917
pub enum OpenEnum<T> {
18+
/// A known value of the generated enum type.
1019
Known(T),
20+
/// An unknown value as decoded from the message.
1121
Unknown(i32),
1222
}
1323

@@ -27,6 +37,9 @@ impl<T> From<T> for OpenEnum<T> {
2737
}
2838

2939
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.
3043
pub fn from_raw(value: i32) -> Self
3144
where
3245
i32: TryInto<T>,
@@ -37,6 +50,7 @@ impl<T> OpenEnum<T> {
3750
}
3851
}
3952

53+
/// Converts an open enum value into its raw integer representation.
4054
pub fn into_raw(self) -> i32
4155
where
4256
T: Into<i32>,
@@ -47,6 +61,9 @@ impl<T> OpenEnum<T> {
4761
}
4862
}
4963

64+
/// Converts an open enum value into its raw integer representation.
65+
///
66+
/// This is a convenience method for borrowed values.
5067
pub fn to_raw(&self) -> i32
5168
where
5269
T: Clone + Into<i32>,
@@ -59,20 +76,36 @@ impl<T> OpenEnum<T> {
5976
}
6077

6178
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.
6284
pub fn unwrap(self) -> T {
6385
match self {
6486
Self::Known(v) => v,
6587
Self::Unknown(v) => panic!("unknown field value {}", v),
6688
}
6789
}
6890

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
6999
pub fn unwrap_or(self, default: T) -> T {
70100
match self {
71101
Self::Known(v) => v,
72102
Self::Unknown(_) => default,
73103
}
74104
}
75105

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.
76109
pub fn unwrap_or_else<F>(self, f: F) -> T
77110
where
78111
F: FnOnce(i32) -> T,
@@ -83,6 +116,8 @@ impl<T> OpenEnum<T> {
83116
}
84117
}
85118

119+
/// Returns the known value of the open enum, or, if the value is unknown,
120+
/// returns the default value of the enum type.
86121
pub fn unwrap_or_default(self) -> T
87122
where
88123
T: Default,
@@ -93,20 +128,33 @@ impl<T> OpenEnum<T> {
93128
}
94129
}
95130

131+
/// If the value of the open enum is known, returns it in `Some`, otherwise
132+
/// returns `None`.
96133
pub fn known(self) -> Option<T> {
97134
match self {
98135
Self::Known(v) => Some(v),
99136
Self::Unknown(_) => None,
100137
}
101138
}
102139

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
103148
pub fn known_or<E>(self, err: E) -> Result<T, E> {
104149
match self {
105150
Self::Known(v) => Ok(v),
106151
Self::Unknown(_) => Err(err),
107152
}
108153
}
109154

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.
110158
pub fn known_or_else<E, F>(self, err: F) -> Result<T, E>
111159
where
112160
F: FnOnce(i32) -> E,

0 commit comments

Comments
 (0)