Skip to content

Commit 6533d56

Browse files
MichaelCuevasfacebook-github-bot
authored andcommitted
PrivHelper: introduce functions for [de]serializing optional values
Summary: # This diff some NFS mount options are optional, and therefore we may need to essentially pass "std::nullopt" across the wire. Since std::optional<T> are not writeable (it is not an arithmetic type), we'll have to invent our own method for serializing/deserializing those values. in this diff, I introduce [de]serializeOptional() functions which will: 1) serialize a bool to indicate whether the optional value is present or not. 2) If the value is present, then we continue to [de]serialize the value 3) If the value is std::nullopt, then we can skip #2 because there's no value to [de]serialize # Context NFS mounts are extremely configurable, especially on macOS. The configurations vary a ton and impact mount perf/reliability in many different ways. Instead of passing around all these mount options as a list of parameters, I decided to clean up the code and pass them around in a struct. That will keep the nfsMount() signature from growing out of hand, and overall make the code cleaner. This clean up is motivated by a desire to add even more NFS Mount Option configurations in the future (which is done later in this stack). Reviewed By: jdelliot Differential Revision: D68602730 fbshipit-source-id: 0e157bac918af0df07e3f0a250c68278bcc412bf
1 parent 7404306 commit 6533d56

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

eden/fs/privhelper/PrivHelperConn.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ UnixSocket::Message serializeRequestPacket(
6868
return msg;
6969
}
7070

71+
// Template function for serializing an optional value of any type that can be
72+
// trivially serialized (i.e. not strings or socket addresses)
73+
template <
74+
typename T,
75+
std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
76+
[[maybe_unused]] void serializeOption(Appender& a, std::optional<T> val) {
77+
bool is_some = val.has_value();
78+
a.write<bool>(is_some);
79+
if (is_some) {
80+
a.write<T>(val.value());
81+
}
82+
}
83+
84+
// Template function for deserializing an optional value of any type that can be
85+
// trivially deserialized (i.e. not strings or socket addresses)
86+
template <
87+
typename T,
88+
std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
89+
[[maybe_unused]] std::optional<T> deserializeOption(Cursor& cursor) {
90+
bool is_some = cursor.read<bool>();
91+
if (is_some) {
92+
return cursor.read<T>();
93+
} else {
94+
return std::nullopt;
95+
}
96+
}
97+
7198
void serializeString(Appender& a, StringPiece str) {
7299
a.write<uint32_t>(str.size());
73100
a.push(ByteRange(str));

0 commit comments

Comments
 (0)