This repository was archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathserialize.rs
More file actions
66 lines (55 loc) · 2.88 KB
/
serialize.rs
File metadata and controls
66 lines (55 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//! Module containing serialization utilities
/// Macro that generates serde::Serialize and serde::Deserialize implementations for the given types.
/// This macro assumes that the types implement zerocopy::FromBytes and zerocopy::AsBytes, and uses
/// these implementations to serialize as opaque byte arrays. During deserialization, it will
/// try to deserialize as a `Vec`. If this deserialized `Vec` has a length that equals `size_of::<T>`,
/// it will transmute to `T` (using zerocopy), otherwise the `Vec` will either be zero-padded, or truncated.
/// This will hopefully allow live update of bindings across kernel versions even if the kernel adds
/// new fields to the end of some struct (we heavily rely on the kernel not making ABI breaking changes here).
macro_rules! serde_impls {
($($typ: ty),*) => {
$(
impl Serialize for $typ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer
{
let bytes = self.as_bytes();
serializer.serialize_bytes(bytes)
}
}
impl<'de> Deserialize<'de> for $typ {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>
{
struct BytesVisitor;
impl<'a> serde::de::Visitor<'a> for BytesVisitor {
type Value = [u8; std::mem::size_of::<$typ>()];
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a byte array")
}
fn visit_bytes<E>(self, bytes: &[u8]) -> Result<Self::Value, E> {
let mut backing = [0u8; std::mem::size_of::<$typ>()];
let limit = bytes.len().min(backing.len());
backing[..limit].copy_from_slice(&bytes[..limit]);
Ok(backing)
}
fn visit_seq<A: serde::de::SeqAccess<'a>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let mut backing = [0u8; std::mem::size_of::<$typ>()];
for backing_byte in &mut backing {
let Some(byte) = seq.next_element()? else { break };
*backing_byte = byte;
}
Ok(backing)
}
}
let backing = deserializer.deserialize_bytes(BytesVisitor)?;
Ok(transmute!(backing))
}
}
)*
}
}