Skip to content

Commit fa22e92

Browse files
committed
xdr_lib: create runtime library for (de)serialize helper functions
1 parent 1af226a commit fa22e92

13 files changed

Lines changed: 119 additions & 105 deletions

File tree

Cargo.lock

Lines changed: 9 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ members = [
77
"tests/alloc",
88
"tests/no_alloc",
99
"xdr_codegen",
10+
"xdr_lib",
1011
]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct File {
4242
}
4343
impl File {
4444
pub fn serialize_alloc(&self) -> Vec<u8>;
45-
pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), helpers::DeserializeError>;
45+
pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), xdr_lib::DeserializeError>;
4646
}
4747
```
4848

nfs3/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rpcbind = { path = "../rpcbind" }
3030
rpc_protocol = { path = "../rpc_protocol" }
3131
log = "0.4.27"
3232
nix = { version = "0.30.1", features = ["socket"] }
33+
xdr_lib = { path = "../xdr_lib" }
3334

3435
[target.'cfg(target_os = "linux")'.dependencies]
3536
libc = "0.2"

rpc_protocol/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ path = "src/lib.rs"
1111
[dependencies]
1212
log = "0.4.27"
1313
nix = { version = "0.30.1", features = ["socket"] }
14+
xdr_lib = { path = "../xdr_lib" }
1415

1516
[build-dependencies]
1617
xdr_codegen = { path = "../xdr_codegen" }

rpcbind/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ env_logger = "0.11.8"
2121
log = "0.4.27"
2222
nix = { version = "0.30.1", features = ["socket"] }
2323
rpc_protocol = { path = "../rpc_protocol" }
24+
xdr_lib = { path = "../xdr_lib" }
2425

2526
[build-dependencies]
2627
xdr_codegen = { path = "../xdr_codegen" }

tests/alloc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ name = "test_alloc"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[dependencies]
7+
xdr_lib = { path = "../../xdr_lib" }
8+
69
[build-dependencies]
710
xdr_codegen = { path = "../../xdr_codegen" }

tests/no_alloc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ name = "test_no_alloc"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[dependencies]
7+
xdr_lib = { path = "../../xdr_lib" }
8+
69
[build-dependencies]
710
xdr_codegen = { path = "../../xdr_codegen" }

xdr_codegen/src/codegen/deserialize.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Array {
1919
}
2020
_ => {
2121
buf.add_line("let mut len = 0;");
22-
buf.add_line("helpers::get_u32(&mut len, input)?;");
22+
buf.add_line("xdr_lib::get_u32(&mut len, input)?;");
2323
}
2424
};
2525
match &self.kind {
@@ -91,7 +91,7 @@ impl NamedDeclaration {
9191
impl ValidatedUnion {
9292
pub(super) fn deserialize_definition(&self, buf: &mut CodeBuf, tab: &ValidatedSymbolTable) {
9393
buf.code_block(
94-
"pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), helpers::DeserializeError>",
94+
"pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), xdr_lib::DeserializeError>",
9595
|buf| {
9696
match &self.body {
9797
ValidatedUnionBody::Bool(b) => b.deserialize_bool(buf, tab),
@@ -106,7 +106,7 @@ impl ValidatedUnion {
106106
impl ValidatedUnionBoolBody {
107107
pub(super) fn deserialize_bool(&self, buf: &mut CodeBuf, tab: &ValidatedSymbolTable) {
108108
buf.add_line("let mut discriminant: u32 = 0;");
109-
buf.add_line("helpers::get_u32(&mut discriminant, input)?;");
109+
buf.add_line("xdr_lib::get_u32(&mut discriminant, input)?;");
110110
buf.block_statement("match discriminant", |buf| {
111111
buf.add_line("0 => (*self).inner = None,");
112112
buf.code_block("_ => ", |buf| {
@@ -124,7 +124,7 @@ impl ValidatedUnionBoolBody {
124124
impl ValidatedUnionEnumBody {
125125
pub(super) fn deserialize_enum(&self, buf: &mut CodeBuf, tab: &ValidatedSymbolTable) {
126126
buf.add_line("let mut discriminant = 0;");
127-
buf.add_line("helpers::get_i32(&mut discriminant, input)?;");
127+
buf.add_line("xdr_lib::get_i32(&mut discriminant, input)?;");
128128
buf.block_statement("*self = match discriminant", |buf| {
129129
for arm in self.arms.iter() {
130130
let discriminant_value = self.get_discriminant_value(&arm.0, tab);
@@ -156,7 +156,7 @@ impl ValidatedUnionEnumBody {
156156
}
157157
};
158158
} else {
159-
buf.add_line("_ => return Err(helpers::DeserializeError),");
159+
buf.add_line("_ => return Err(xdr_lib::DeserializeError),");
160160
}
161161
});
162162
}
@@ -165,7 +165,7 @@ impl ValidatedUnionEnumBody {
165165
impl ValidatedStruct {
166166
pub(super) fn deserialize_definition(&self, buf: &mut CodeBuf, tab: &ValidatedSymbolTable) {
167167
buf.code_block(
168-
"pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), helpers::DeserializeError>",
168+
"pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), xdr_lib::DeserializeError>",
169169
|buf| {
170170
for (decl, _) in self.members.iter() {
171171
buf.add_line(&format!("// {}:", decl.name));
@@ -180,16 +180,16 @@ impl ValidatedStruct {
180180
impl ValidatedEnum {
181181
pub(super) fn deserialize_definition(&self, buf: &mut CodeBuf, tab: &ValidatedSymbolTable) {
182182
buf.code_block(
183-
"pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), helpers::DeserializeError>",
183+
"pub fn deserialize(&mut self, input: &mut &[u8]) -> Result<(), xdr_lib::DeserializeError>",
184184
|buf| {
185185
buf.add_line("let mut val = 0;");
186-
buf.add_line("helpers::get_i32(&mut val, input)?;");
186+
buf.add_line("xdr_lib::get_i32(&mut val, input)?;");
187187
buf.block_statement("*self = match val", |buf| {
188188
for variant in self.variants.iter() {
189189
let val = variant.1.as_const(tab);
190190
buf.add_line(&format!("{} => {}::{},", val, self.name, variant.0));
191191
}
192-
buf.add_line("_ => return Err(helpers::DeserializeError),");
192+
buf.add_line("_ => return Err(xdr_lib::DeserializeError),");
193193
});
194194
buf.add_line("Ok(())");
195195
},
@@ -220,14 +220,14 @@ impl XdrType {
220220

221221
fn deserialize_method(&self) -> String {
222222
match self {
223-
XdrType::Int => "helpers::get_i32".to_string(),
224-
XdrType::UInt => "helpers::get_u32".to_string(),
225-
XdrType::Hyper => "helpers::get_i64".to_string(),
226-
XdrType::UHyper => "helpers::get_u64".to_string(),
223+
XdrType::Int => "xdr_lib::get_i32".to_string(),
224+
XdrType::UInt => "xdr_lib::get_u32".to_string(),
225+
XdrType::Hyper => "xdr_lib::get_i64".to_string(),
226+
XdrType::UHyper => "xdr_lib::get_u64".to_string(),
227227
XdrType::Float => todo!(),
228228
XdrType::Double => todo!(),
229229
XdrType::Quadruple => todo!(),
230-
XdrType::Bool => "helpers::get_bool".to_string(),
230+
XdrType::Bool => "xdr_lib::get_bool".to_string(),
231231
XdrType::Name(n) => format!("{n}::deserialize"),
232232
}
233233
}
@@ -241,15 +241,15 @@ impl XdrType {
241241
if self.self_referential_optional(tab) {
242242
buf.code_block("loop", |buf| {
243243
buf.add_line("let mut item_follows = 0;");
244-
buf.add_line("helpers::get_i32(&mut item_follows, input)?;");
244+
buf.add_line("xdr_lib::get_i32(&mut item_follows, input)?;");
245245
buf.add_line("if item_follows == 0 { break; }");
246246
buf.add_line(&format!("let mut new = {};", self.default_value(tab)));
247247
self.deserialize_inline("new", buf, tab);
248248
buf.add_line(&format!("{name}.push(new)"));
249249
});
250250
} else {
251251
buf.add_line("let mut optional_follows = 0;");
252-
buf.add_line("helpers::get_i32(&mut optional_follows, input)?;");
252+
buf.add_line("xdr_lib::get_i32(&mut optional_follows, input)?;");
253253
buf.block_statement(&format!("{name} = match optional_follows"), |buf| {
254254
buf.add_line("0 => None,");
255255
buf.code_block("_ =>", |buf| {

xdr_codegen/src/codegen/mod.rs

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -31,85 +31,6 @@ impl Default for Params {
3131
}
3232
}
3333

34-
const HELPERS: &str = r#"
35-
pub fn get_i32(dst: &mut i32, input: &mut &[u8]) -> Result<(), DeserializeError> {
36-
if input.len() < 4 {
37-
return Err(DeserializeError);
38-
}
39-
let (int_bytes, rest) = input.split_at(std::mem::size_of::<i32>());
40-
*input = rest;
41-
*dst = i32::from_be_bytes(int_bytes.try_into().unwrap());
42-
Ok(())
43-
}
44-
45-
pub fn get_u32(dst: &mut u32, input: &mut &[u8]) -> Result<(), DeserializeError> {
46-
if input.len() < 4 {
47-
return Err(DeserializeError);
48-
}
49-
let (int_bytes, rest) = input.split_at(std::mem::size_of::<u32>());
50-
*input = rest;
51-
*dst = u32::from_be_bytes(int_bytes.try_into().unwrap());
52-
Ok(())
53-
}
54-
55-
pub fn get_i64(dst: &mut i64, input: &mut &[u8]) -> Result<(), DeserializeError> {
56-
if input.len() < 4 {
57-
return Err(DeserializeError);
58-
}
59-
let (int_bytes, rest) = input.split_at(std::mem::size_of::<i64>());
60-
*input = rest;
61-
*dst = i64::from_be_bytes(int_bytes.try_into().unwrap());
62-
Ok(())
63-
}
64-
65-
pub fn get_u64(dst: &mut u64, input: &mut &[u8]) -> Result<(), DeserializeError> {
66-
if input.len() < 4 {
67-
return Err(DeserializeError);
68-
}
69-
let (int_bytes, rest) = input.split_at(std::mem::size_of::<u64>());
70-
*input = rest;
71-
*dst = u64::from_be_bytes(int_bytes.try_into().unwrap());
72-
Ok(())
73-
}
74-
75-
pub fn get_bool(dst: &mut bool, input: &mut &[u8]) -> Result<(), DeserializeError> {
76-
if input.len() < 4 {
77-
return Err(DeserializeError);
78-
}
79-
let (bool_bytes, rest) = input.split_at(std::mem::size_of::<u32>());
80-
*input = rest;
81-
*dst = match u32::from_be_bytes(bool_bytes.try_into().unwrap()) {
82-
0 => false,
83-
_ => true,
84-
};
85-
Ok(())
86-
}
87-
88-
pub fn serialize_bool(src: &bool) -> [u8; 4] {
89-
match src {
90-
true => 1_u32.to_be_bytes(),
91-
false => 0_u32.to_be_bytes(),
92-
}
93-
}
94-
95-
pub fn encode_padding(offset: usize, buf: &mut [u8]) -> usize {
96-
let padded_offset: usize = (offset + 3) & !(0b11usize);
97-
buf[offset..padded_offset].fill(0u8);
98-
padded_offset
99-
}
100-
101-
#[derive(Debug)]
102-
pub struct DeserializeError;
103-
104-
impl std::error::Error for DeserializeError {}
105-
106-
impl std::fmt::Display for DeserializeError {
107-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
108-
write!(f, "Invalid input to deserialize method")
109-
}
110-
}
111-
"#;
112-
11334
const USE_FFI_HEADER: &str = r#"
11435
use std::os::unix::ffi::OsStrExt;
11536
"#;
@@ -142,13 +63,6 @@ pub fn codegen(schema: &ValidatedSchema, module_name: &str, params: &Params) ->
14263
for prog in schema.programs.iter() {
14364
prog.codegen(buf);
14465
}
145-
146-
buf.add_line("#[allow(dead_code)]");
147-
buf.code_block("mod helpers", |buf| {
148-
for line in HELPERS.lines() {
149-
buf.add_line(line);
150-
}
151-
});
15266
});
15367

15468
buf.contents
@@ -761,7 +675,7 @@ impl XdrType {
761675
XdrType::Quadruple => todo!(),
762676
XdrType::Bool => {
763677
return (
764-
"helpers::serialize_bool".to_string(),
678+
"xdr_lib::serialize_bool".to_string(),
765679
FunctionKind::Function,
766680
)
767681
}

0 commit comments

Comments
 (0)