Skip to content

Commit 3fee552

Browse files
committed
feat: get, set methods for OpenEnum
These are meant to be the primary ways to get and set open enumeration fields, instead of the getters and setters generated by Message derive.
1 parent e4c84e7 commit 3fee552

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

prost/src/open_enum.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::encoding::{DecodeContext, WireType};
2-
use crate::{DecodeError, Message};
2+
use crate::{DecodeError, Message, UnknownEnumValue};
33

44
use bytes::{Buf, BufMut};
55

@@ -128,6 +128,23 @@ impl<T> OpenEnum<T> {
128128
}
129129
}
130130

131+
/// If the value of the open enum is known, returns it in `Ok`, otherwise
132+
/// returns an `Err` with the unknown value.
133+
pub fn get(&self) -> Result<T, UnknownEnumValue>
134+
where
135+
T: Clone,
136+
{
137+
match self {
138+
Self::Known(v) => Ok(v.clone()),
139+
Self::Unknown(r) => Err(UnknownEnumValue(*r)),
140+
}
141+
}
142+
143+
/// Sets the value of receiver to the provided known value.
144+
pub fn set(&mut self, value: T) {
145+
*self = Self::Known(value);
146+
}
147+
131148
/// If the value of the open enum is known, returns it in `Some`, otherwise
132149
/// returns `None`.
133150
pub fn known(self) -> Option<T> {

0 commit comments

Comments
 (0)