Skip to main content

proto_types/
empty.rs

1use prost::Name;
2
3use crate::{String, constants::PACKAGE_PREFIX, type_url_for};
4
5/// A generic empty message that you can re-use to avoid defining duplicated
6/// empty messages in your APIs.
7///
8/// A typical example is to use it as the request
9/// or the response type of an API method. For instance:
10///
11/// ```proto
12/// service Foo {
13///   rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
14/// }
15/// ```
16///
17#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
18pub struct Empty;
19
20impl ::prost::Message for Empty {
21	fn encode_raw(&self, _: &mut impl ::prost::bytes::BufMut) {}
22	fn merge_field(
23		&mut self,
24		tag: u32,
25		wire_type: ::prost::encoding::wire_type::WireType,
26		buf: &mut impl ::prost::bytes::Buf,
27		ctx: ::prost::encoding::DecodeContext,
28	) -> ::core::result::Result<(), ::prost::DecodeError> {
29		::prost::encoding::skip_field(wire_type, tag, buf, ctx)
30	}
31	#[inline]
32	fn encoded_len(&self) -> usize {
33		0
34	}
35	fn clear(&mut self) {}
36}
37
38impl From<()> for Empty {
39	fn from((): ()) -> Self {
40		Self {}
41	}
42}
43
44impl Name for Empty {
45	const PACKAGE: &'static str = PACKAGE_PREFIX;
46
47	const NAME: &'static str = "Empty";
48
49	fn type_url() -> String {
50		type_url_for::<Self>()
51	}
52}
53
54#[cfg(feature = "serde")]
55mod serde_impls {
56	use super::*;
57	use crate::format;
58	use core::fmt;
59
60	use serde::{Deserialize, Serialize, ser::SerializeStruct};
61
62	use crate::Empty;
63	impl Serialize for Empty {
64		fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
65		where
66			S: serde::Serializer,
67		{
68			// Serialize as an empty struct (which maps to an empty JSON object `{}`)
69			serializer.serialize_struct("Empty", 0)?.end()
70		}
71	}
72
73	impl<'de> Deserialize<'de> for Empty {
74		fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
75		where
76			D: serde::Deserializer<'de>,
77		{
78			struct EmptyVisitor;
79
80			impl<'de> serde::de::Visitor<'de> for EmptyVisitor {
81				type Value = Empty;
82
83				fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
84					formatter.write_str("an empty object `{}`")
85				}
86
87				fn visit_map<A>(self, mut _map: A) -> Result<Self::Value, A::Error>
88				where
89					A: serde::de::MapAccess<'de>,
90				{
91					// Ensure there are no unexpected fields in the map
92					if let Some(key) = _map.next_key::<String>()? {
93						return Err(serde::de::Error::custom(format!(
94							"Unexpected field in Empty message: {key}"
95						)));
96					}
97					Ok(Empty {})
98				}
99
100				// Also allow deserializing from unit (`()`) if needed, though `{}` is standard for JSON
101				fn visit_unit<E>(self) -> Result<Self::Value, E>
102				where
103					E: serde::de::Error,
104				{
105					Ok(Empty {})
106				}
107			}
108
109			deserializer.deserialize_unit_struct("Empty", EmptyVisitor) // Expect a struct with no fields
110		}
111	}
112}