Skip to content

Commit af5a45e

Browse files
committed
[hyperactor] Serialized: multipart support
Pull Request resolved: #905 ghstack-source-id: 303723939 @exported-using-ghexport Differential Revision: [D80447202](https://our.internmc.facebook.com/intern/diff/D80447202/)
1 parent 1c615bc commit af5a45e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

hyperactor/src/data.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ macro_rules! register_type {
302302
enum Encoded {
303303
Bincode(serde_bytes::ByteBuf),
304304
Json(serde_bytes::ByteBuf),
305-
// todo: multipart
305+
Multipart(serde_multipart::Message),
306306
}
307307

308308
impl Encoded {
@@ -311,6 +311,7 @@ impl Encoded {
311311
match &self {
312312
Encoded::Bincode(data) => data.len(),
313313
Encoded::Json(data) => data.len(),
314+
Encoded::Multipart(message) => message.len(),
314315
}
315316
}
316317

@@ -319,6 +320,7 @@ impl Encoded {
319320
match &self {
320321
Encoded::Bincode(data) => data.is_empty(),
321322
Encoded::Json(data) => data.is_empty(),
323+
Encoded::Multipart(message) => message.is_empty(),
322324
}
323325
}
324326

@@ -327,6 +329,14 @@ impl Encoded {
327329
match &self {
328330
Encoded::Bincode(data) => crc32fast::hash(data),
329331
Encoded::Json(data) => crc32fast::hash(data),
332+
Encoded::Multipart(message) => {
333+
let mut hasher = crc32fast::Hasher::new();
334+
hasher.update(message.body().as_ref());
335+
for part in message.parts() {
336+
hasher.update(part.as_ref());
337+
}
338+
hasher.finalize()
339+
}
330340
}
331341
}
332342
}
@@ -336,6 +346,7 @@ impl std::fmt::Debug for Encoded {
336346
match self {
337347
Encoded::Bincode(data) => write!(f, "Encoded::Bincode({})", HexFmt(data.as_slice())),
338348
Encoded::Json(data) => write!(f, "Encoded::Json({})", HexFmt(data.as_slice())),
349+
Encoded::Multipart(message) => todo!(), //write!(f, "Encoded::Multipart({})", HexFmt(data.as_slice())),
339350
}
340351
}
341352
}
@@ -392,14 +403,17 @@ impl Serialized {
392403
match &self.encoded {
393404
Encoded::Bincode(data) => bincode::deserialize(data).map_err(anyhow::Error::from),
394405
Encoded::Json(data) => serde_json::from_slice(data).map_err(anyhow::Error::from),
406+
Encoded::Multipart(message) => {
407+
serde_multipart::deserialize_bincode(message.clone()).map_err(anyhow::Error::from)
408+
}
395409
}
396410
}
397411

398412
/// Transcode the serialized value to JSON. This operation will succeed if the type hash
399413
/// is embedded in the value, and the corresponding type is available in this binary.
400414
pub fn transcode_to_json(self) -> Result<Self, Self> {
401415
match self.encoded {
402-
Encoded::Bincode(_) => {
416+
Encoded::Bincode(_) | Encoded::Multipart(_) => {
403417
let json_value = match self.dump() {
404418
Ok(json_value) => json_value,
405419
Err(_) => return Err(self),
@@ -431,6 +445,10 @@ impl Serialized {
431445
typeinfo.dump(self.clone())
432446
}
433447
Encoded::Json(data) => serde_json::from_slice(data).map_err(anyhow::Error::from),
448+
Encoded::Multipart(_) => {
449+
// TODO: implement typeinfo.dump_multipart
450+
anyhow::bail!("dumping multipart-encoded values is not yet supported")
451+
}
434452
}
435453
}
436454

0 commit comments

Comments
 (0)