Skip to content
This repository was archived by the owner on Jun 15, 2020. It is now read-only.

Commit e1a4453

Browse files
committed
feat(wasm) Add the push_u8s macro.
1 parent 58d8c1f commit e1a4453

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

bindings/wasm/src/lib.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ pub extern "C" fn dealloc(pointer: *mut c_void, capacity: usize) {
7777
}
7878
}
7979

80+
macro_rules! push_u8s {
81+
($u8s:ident in $output:ident) => (
82+
$output.push($u8s.0);
83+
$output.push($u8s.1);
84+
$output.push($u8s.2);
85+
$output.push($u8s.3);
86+
)
87+
}
88+
8089
#[no_mangle]
8190
pub extern "C" fn root(pointer: *mut u8, length: usize) -> *mut u8 {
8291
let input = unsafe { slice::from_raw_parts(pointer, length) };
@@ -87,10 +96,7 @@ pub extern "C" fn root(pointer: *mut u8, length: usize) -> *mut u8 {
8796
if let Ok((_remaining, nodes)) = gutenberg_post_parser::root(input) {
8897
let nodes_length = u32_to_u8s(nodes.len() as u32);
8998

90-
output.push(nodes_length.0);
91-
output.push(nodes_length.1);
92-
output.push(nodes_length.2);
93-
output.push(nodes_length.3);
99+
push_u8s!(nodes_length in output);
94100

95101
for node in nodes {
96102
into_bytes(&node, &mut output);
@@ -125,10 +131,7 @@ fn into_bytes<'a>(node: &Node<'a>, output: &mut Vec<u8>) {
125131

126132
output.push(node_type);
127133
output.push(name_length as u8);
128-
output.push(attributes_length_as_u8s.0);
129-
output.push(attributes_length_as_u8s.1);
130-
output.push(attributes_length_as_u8s.2);
131-
output.push(attributes_length_as_u8s.3);
134+
push_u8s!(attributes_length_as_u8s in output);
132135
output.push(number_of_children as u8);
133136

134137
output.extend(name.0);
@@ -149,16 +152,10 @@ fn into_bytes<'a>(node: &Node<'a>, output: &mut Vec<u8>) {
149152
Node::Phrase(phrase) => {
150153
let node_type = 2u8;
151154
let phrase_length = phrase.len();
152-
153-
output.push(node_type);
154-
155155
let phrase_length_as_u8s = u32_to_u8s(phrase_length as u32);
156156

157-
output.push(phrase_length_as_u8s.0);
158-
output.push(phrase_length_as_u8s.1);
159-
output.push(phrase_length_as_u8s.2);
160-
output.push(phrase_length_as_u8s.3);
161-
157+
output.push(node_type);
158+
push_u8s!(phrase_length_as_u8s in output);
162159
output.extend(phrase);
163160
}
164161
}

0 commit comments

Comments
 (0)