Skip to content
This repository was archived by the owner on Nov 6, 2024. It is now read-only.

Commit d26619a

Browse files
author
Alexandra Iordache
committed
serde: add [de]serialization for bindings
* use #[derive(Serialize,Deserialize)] where possible * script that adds it wherever it can be auto-derived * manually impl for unions and structs with fixed size array members * impl [De]Serialize for compatibility with bincode * CAUTION! no compatibility with serde_json! Signed-off-by: Alexandra Iordache <[email protected]>
1 parent 59b969f commit d26619a

File tree

9 files changed

+1496
-245
lines changed

9 files changed

+1496
-245
lines changed

Cargo.lock

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ kvm-v4_14_0 = []
1313
kvm-v4_20_0 = []
1414

1515
[dependencies]
16+
17+
[target.'cfg(any(target_arch = "x86", target_arch = "x86_64"))'.dependencies]
18+
serde = ">=1.0.27"
19+
serde_derive = ">=1.0.27"

add_serde.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from glob import glob
2+
import fileinput
3+
4+
5+
BLACKLIST = [
6+
'kvm_enable_cap ',
7+
'kvm_hyperv_exit__bindgen_ty_1__bindgen_ty_2 ',
8+
'kvm_ioapic_state ',
9+
'kvm_ioeventfd ',
10+
'kvm_lapic_state ',
11+
'kvm_ppc_pvinfo ',
12+
'kvm_xsave ',
13+
'union '
14+
]
15+
16+
17+
def println(line):
18+
print(line, end='')
19+
20+
21+
def main():
22+
with fileinput.input(files=glob('src/x86/bindings_*.rs'),
23+
inplace=True, backup='.bak',) as src:
24+
for line in src:
25+
# Add Serialize & Deserialize where necessary.
26+
if line.startswith('#[derive('):
27+
# Skip if Serialize & Deserialize were manually added.
28+
if 'Serialize' in line:
29+
println(line)
30+
continue
31+
32+
# Don't derive [De]Serialize on structs that don't support it.
33+
next_line = src.readline()
34+
ok_to_derive = True
35+
for not_allowed in BLACKLIST:
36+
if not_allowed in next_line:
37+
ok_to_derive = False
38+
break
39+
if ok_to_derive:
40+
println(line.replace(')]', ', Serialize, Deserialize)]'))
41+
else:
42+
println(line)
43+
println(next_line)
44+
45+
else:
46+
println(line)
47+
48+
49+
if __name__ == '__main__':
50+
main()

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
mod x86;
1010
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1111
pub use self::x86::bindings::*;
12+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
13+
extern crate serde;
14+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
15+
#[macro_use]
16+
extern crate serde_derive;
1217

1318
#[cfg(target_arch = "aarch")]
1419
mod arm;

0 commit comments

Comments
 (0)