Skip to content

Commit 3381a87

Browse files
committed
[hyperactor] Serialized: multipart support
Differential Revision: [D80447202](https://our.internmc.facebook.com/intern/diff/D80447202/) ghstack-source-id: 303699219 Pull Request resolved: #905
1 parent 0074e6e commit 3381a87

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
@@ -296,7 +296,7 @@ macro_rules! register_type {
296296
enum Encoded {
297297
Bincode(serde_bytes::ByteBuf),
298298
Json(serde_bytes::ByteBuf),
299-
// todo: multipart
299+
Multipart(serde_multipart::Message),
300300
}
301301

302302
impl Encoded {
@@ -305,6 +305,7 @@ impl Encoded {
305305
match &self {
306306
Encoded::Bincode(data) => data.len(),
307307
Encoded::Json(data) => data.len(),
308+
Encoded::Multipart(message) => message.len(),
308309
}
309310
}
310311

@@ -313,6 +314,7 @@ impl Encoded {
313314
match &self {
314315
Encoded::Bincode(data) => data.is_empty(),
315316
Encoded::Json(data) => data.is_empty(),
317+
Encoded::Multipart(message) => message.is_empty(),
316318
}
317319
}
318320

@@ -321,6 +323,14 @@ impl Encoded {
321323
match &self {
322324
Encoded::Bincode(data) => crc32fast::hash(data),
323325
Encoded::Json(data) => crc32fast::hash(data),
326+
Encoded::Multipart(message) => {
327+
let mut hasher = crc32fast::Hasher::new();
328+
hasher.update(message.body().as_ref());
329+
for part in message.parts() {
330+
hasher.update(part.as_ref());
331+
}
332+
hasher.finalize()
333+
}
324334
}
325335
}
326336
}
@@ -330,6 +340,7 @@ impl std::fmt::Debug for Encoded {
330340
match self {
331341
Encoded::Bincode(data) => write!(f, "Encoded::Bincode({})", HexFmt(data.as_slice())),
332342
Encoded::Json(data) => write!(f, "Encoded::Json({})", HexFmt(data.as_slice())),
343+
Encoded::Multipart(message) => todo!(), //write!(f, "Encoded::Multipart({})", HexFmt(data.as_slice())),
333344
}
334345
}
335346
}
@@ -386,14 +397,17 @@ impl Serialized {
386397
match &self.encoded {
387398
Encoded::Bincode(data) => bincode::deserialize(data).map_err(anyhow::Error::from),
388399
Encoded::Json(data) => serde_json::from_slice(data).map_err(anyhow::Error::from),
400+
Encoded::Multipart(message) => {
401+
serde_multipart::deserialize_bincode(message.clone()).map_err(anyhow::Error::from)
402+
}
389403
}
390404
}
391405

392406
/// Transcode the serialized value to JSON. This operation will succeed if the type hash
393407
/// is embedded in the value, and the corresponding type is available in this binary.
394408
pub fn transcode_to_json(self) -> Result<Self, Self> {
395409
match self.encoded {
396-
Encoded::Bincode(_) => {
410+
Encoded::Bincode(_) | Encoded::Multipart(_) => {
397411
let json_value = match self.dump() {
398412
Ok(json_value) => json_value,
399413
Err(_) => return Err(self),
@@ -425,6 +439,10 @@ impl Serialized {
425439
typeinfo.dump(self.clone())
426440
}
427441
Encoded::Json(data) => serde_json::from_slice(data).map_err(anyhow::Error::from),
442+
Encoded::Multipart(_) => {
443+
// TODO: implement typeinfo.dump_multipart
444+
anyhow::bail!("dumping multipart-encoded values is not yet supported")
445+
}
428446
}
429447
}
430448

0 commit comments

Comments
 (0)